How to numerically sort an array of strings? [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have an array of strings that has a number at the beginning, followed by a name. How can I sort the array numerically; ie
40 Mike, 25 Tyson, 9 Jackson, 5 Phillip, 3 Mitchell
as opposed to
9 Jackson, 5 Phillip, 40 Mike, 3 Mitchell, 25 Tyson

using System;
using System.Collections.Generic;
public class Test
{
public static void Main()
{
string[] items = {"10", "1", "30", "-5"};
Array.Sort(items, (x, y) =>
{
int xNum = int.Parse(x);
int yNum = int.Parse(y);
return Comparer<int>.Default.Compare(xNum, yNum);
});
foreach(var item in items)
Console.WriteLine(item);
}
}

var sorted = yourArray.OrderBy(x => int.Parse(x.Split()[0])).ToArray();

You can use SortedDictionary data structure to obtain an array sorted.

Related

How to add List's all values? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 months ago.
Improve this question
of course I can use for() {}
but is there any function which can add all values in array?
if do you know, please tell me.
thank you.
int[] a = new int[] { 1, 2, 3, 4, 5 };
int[] b = new int[a.Length];
a.CopyTo(b, 0);
b.ToList().ForEach(c=>{
System.Console.WriteLine(c);
});
using System.Linq;
then...
var array = new double[] { 1.1, 2.2, 3.3 };
var sum = array.Sum(value=>value);
Console.WriteLine($"Sum is {sum}");

Regular expression for value Range from 1 - 1440 (Integer) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
What will be the regular expression for value Range from 1 - 1440? (Integer)
I think this will do it. I'm running tests. I'm acting under the assumption you will not have numbers padded with zero like "0999"
"^([1-9]|[1-9][0-9]|[1-9][0-9][0-9]|1[0-3][0-9][0-9]|14[0-3][0-9]|1440)$"
breakdown:
[1-9] obvious
[1-9[0-9] allow 10 to 19
[1-9][0-9][0-9] allow 100 to 199
1[0-3][0-9][0-9] allow 1000 to 1399
14[0-3][0-9] allow 1400 to 1439
1440 obvious
Appears to work:
using System;
using System.Text.RegularExpressions;
public class Program
{
public static void Main()
{
for(int i = 0;i < 10000;i++)
{
if(Regex.IsMatch(i.ToString(),"^([1-9]|[1-9][0-9]|[1-9][0-9][0-9]|1[0-3][0-9][0-9]|14[0-3][0-9]|1440)$"))
{
Console.WriteLine(i);
}
}
}
}

int permutations in C# [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I want to implement a method with the following signature:
public int Max(int number){ }
The largest number that can be created by a digits of a given number is obtained by ordering the digits from largest to smallest. See the following code for a possible implementation.
public int Max(int number)
{
var numberAsCharArray = number.ToString().OrderByDescending(c => c).ToArray();
var largestNumberAsString = new string(numberAsCharArray);
return Int32.Parse(str);
}
Maybe not most efficient way is to cast to string and order desc
var result = int.Parse(String.Join("", digit.ToString().OrderByDescending(x => x)))

How to write arrays to tab delimited text file? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have two arrays -
int[] array1 = { 1, 2};
int[] array2 = { 6, 7};
I want to write them in text file delimited by tab.
Final Result -
1 6
2 7
Please suggest how to do this?
Assuming both arrays are of the same length:
File.WriteAllLines(filename,
Enumerable.Range(0, arr1.Length)
.Select(i => string.Format("{0}\t{1}", arr1[i], arr2[i]));
You could do something like this:
public void writeFile(int[] array){
using (streamWriter sw = new StreamWriter(filename, true){
for(int i = 0; i < array.Length; i++){
sw.WriteLine(array[i] + "\t");
}
}
}
Then you would just call the write method like this:
writeFile(array1);
writeFile(array2);

How to call my int array class to ad my dropdownlist [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
public static int[] ddDuration()
{
int[] ddarray = new int[] { 1, 2, 3, 4, 5 };
return ddarray;
}
How to make in c# to call my int array to who any page in my DropDownList
//and I am try to show in my DropDownList
//like that some thing
Duration dm = new Duration();
//dm.ddDuration;
//dropdduration.item.AddRang(dm.ddDuration);
If I got this right, you're trying to add an array of integers as items to a DropDownList.
var collection = ddDuration();
int len = collection.length;
for(int i=0; i<len; i++){
DropDownList1.Items.Insert(i, new ListItem(collection[i].toString(), ""));
}
Insert's first parameter is the position (index) in the Items collection and the second parameter takes a ListItem; of which constructor can be build upon its content (string in this case).

Categories

Resources