我有一个ArrayList
包含多个String[]
(字符串数组)的。
每个都String[]
包含一个键:name
我想做的是ArrayList
根据name
.
使用Array.Sort
不起作用,因为它无法比较String[]
。我如何ArrayList
根据它的值对它name
进行排序String[]
?
In LINQ:
var ordered = arrayList.Cast<String[]>().OrderBy(ar => arr[0]);
Normally it's better to use a strong typed List<String[]>
instead of an ArrayList
. When you say key it sounds as if you want to have a list of strings for each unique key. Then a Dictionary<String, String[]>
would be the best choice.
You should be using a List<string[]>
. It supports everything that ArrayList
does, and it is strongly typed.
Using a List<string[]>
:
IEnumerable<string[]> sorted = myList.OrderBy(s => s[0]);
尝试这个
ArrayList arr = new ArrayList();
arr.ToArray().OrderBy(p => p);
使用Array.Sort(Array,IComparer)并编写您自己的比较器,在您的 String[] 中查找您的键并比较它们。
您可以创建IComparer
一个使用
public virtual void Sort(
IComparer comparer
)
You can pass to Array.Sort
either a delegate or a class implementing an interface that implements the custom comparison you need.
See some examples http://bytes.com/topic/c-sharp/answers/235679-two-dimension-array-sort.