0

I'm trying to sort my string array but i have no idea how to do it :( I have a list that looks like this : enter image description here

On fields is a name and one is a value, all of them are strings. I need to sort this list by value and return 14 names that have the biggest value. The regular sorting cannot help me with this so maybe one of you have an idea how I can do it?

using (StreamReader reader = new StreamReader(response.Stream))
{
    string line;
    while ((line = reader.ReadLine()) != null)
    {
        resultsList.Add(line);
    }
}

var list= new List<string[]>();
foreach (var row in resultsList)
{
    var temp = row.Split(new string[] { "\t" }, StringSplitOptions.None);
    list.Add(temp);
}
5
  • Do you have array or ArrayList? Commented Dec 31, 2015 at 12:34
  • 6
    I don't even understand what your problem is to be honest, but it looks very much like you're using the wrong data structure for the job at hand. Commented Dec 31, 2015 at 12:34
  • Please do post your current code and used classes. Commented Dec 31, 2015 at 12:39
  • 2
    you need to create a class for this. with two properties. to hold name and values. because your elements have special meaning dont put them in string array. Commented Dec 31, 2015 at 12:43
  • I updated the cod , hope it will help Commented Dec 31, 2015 at 13:24

5 Answers 5

2

Assuming you have a List and your value is always the second element of the array then you can sort the list like this

list.Sort((sa1, sa2) => sa1[1].CompareTo(sa2[1]));

If the value can be else than 0-9 you should convert it to int.

list.Sort((sa1, sa2) => Int32.Parse(sa1[1]).CompareTo(Int32.Parse(sa2[1])));

Of course the right thing to do is to build a list of some class instead of use list of array and depend on the array structure.

Sign up to request clarification or add additional context in comments.

Comments

1

Looks like you have something like:

    string[][] arrayOfStringArrays = new string[5][];
    arrayOfStringArrays[0] = new string[] { "name1", "value1" };
    arrayOfStringArrays[1] = new string[] { "name2", "value2" };
    arrayOfStringArrays[2] = new string[] { "name3", "value3" };
    arrayOfStringArrays[3] = new string[] { "name4", "value4" };
    arrayOfStringArrays[4] = new string[] { "name5", "value5" };

You could then do something like:

    var sortedValues = arrayOfStringArrays.Select(e => e.LastorDefault()).OrderBy(s => s);

    //created a separate variable for "n" just for illustration purposes
    var n = 14;
    var firstNElements = sortedValues.Take(n);

Comments

1
list.OrderByDescending(x=>x[1]).Take(14)

The OrderBy section tells it to take the initial array and sort it according to the second element of each inner array. The Take then limits the result set to the 14 you want.

This is different from the Sort solution since it leaves the original list in the same order.

Comments

0
list.OrderByDescending(o => o[1]).Take(14);

You can use OrderByDescending and specify the second array value as sorting "property". Then take the top 14.

Comments

0

You could convert the list to a dictionary with string keys and int values, which makes the selection easier:

        Dictionary<string,int> dict = 
            list.Select(i => new KeyValuePair<string, int>(i[0], Convert.ToInt32(i[1]))).ToDictionary(kv=>kv.Key, kv=>kv.Value);
        var items = dict.OrderByDescending(d => d.Value).Select(d => d.Key).Take(14);

Otherwise, you can directly select from the list, but as I assume you are using string[][] you cannot use OrderBy. Therefore, a custom comparer is needed:

    public class ListComparer : IComparer<string>
    {
        public int Compare(string x, string y)
        {
            if (x == null)
            {
                if (y == null)
                {
                    // If x is null and y is null, they're
                    // equal. 
                    return 0;
                }
                else
                {
                    // If x is null and y is not null, y
                    // is greater. 
                    return -1;
                }
            }
            else
            {
                // If x is not null...
                //
                if (y == null)
                // ...and y is null, x is greater.
                {
                    return 1;
                }
                else
                {
                    // ...and y is not null, compare the 
                    // int values of the two strings.
                    //
                    return Convert.ToInt32(x).CompareTo(Convert.ToInt32(y));
                }
            }
        }
    }

Then you are able to use

var sortedList = list.OrderByDescending(d => d[1], new ListComparer()).Take(14);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.