Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort List<String[]>

Tags:

c#

How do you sort a List alphanumerically by the value in string[0]?

like image 300
Joe Smith Avatar asked Jan 20 '11 19:01

Joe Smith


People also ask

How do you sort a list of strings?

sort() list provides a member function sort(). It Sorts the elements of list in low to high order i.e. if list is of numbers then by default they will be sorted in increasing order. Whereas, if list is of strings then, it will sort them in alphabetical order.

Can sort () sort strings?

sort() method. It sorts the specified array of strings into ascending order, according to the natural ordering of its elements. It uses dual-pivot Quicksort, which is typically faster than traditional single-pivot Quicksort.

How do you sort two strings alphabetically in Python?

Summary. Use the Python List sort() method to sort a list in place. The sort() method sorts the string elements in alphabetical order and sorts the numeric elements from smallest to largest. Use the sort(reverse=True) to reverse the default sort order.

How do you sort a list in C#?

Sort() Method Set -1. List<T>. Sort() Method is used to sort the elements or a portion of the elements in the List<T> using either the specified or default IComparer<T> implementation or a provided Comparison<T> delegate to compare list elements.


1 Answers

Try

list.Sort((s, t) => String.Compare(s[0], t[0]));

This will sort lexicographically by the first element of each array in list.

Since I don't know exactly what you mean by "alphanumerically", if you need a custom string comparing routing, you should do this:

class MyStringComparer : IComparer<string> {
    public int Compare(string s, string t) {
        // details elided
    }
}

and then

var comparer = new MyStringComparer();
list.Sort((s, t) => comparer.Compare(s[0], t[0]));
like image 81
jason Avatar answered Sep 22 '22 03:09

jason