开发者

Sort List<String[]>

How do y开发者_如何转开发ou sort a List alphanumerically by the value in string[0]?


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]));


This is a terribly oversimplified/pseudo code example... Really stretching here...

MyclassZeroIndexComparer : IComparable, IList(Of String[])
{
  private List<String[]> listOfStringArrays;

  //expose this as a public member somewhere...
  private List<String[]> listofStringArrayZeroIndex; 

  //internally store a list of only the first item you want to index....
  //implement your own, add,remove,clear

  void Add(foo item)
  {
    listofStringArrays.Add(Item);
    listofStringArrayZeroIndex.add(item[0]); 
  }

  // continue implementing other methods in the same manner.
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜