0

我有一个ArrayList包含多个String[](字符串数组)的。

每个都String[]包含一个键:name

我想做的是ArrayList根据name.

使用Array.Sort不起作用,因为它无法比较String[]。我如何ArrayList根据它的值对它name进行排序String[]

4

6 回答 6

4

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.

于 2012-04-19T12:15:12.473 回答
1

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]);
于 2012-04-19T12:15:17.773 回答
0

尝试这个

ArrayList arr = new ArrayList();
arr.ToArray().OrderBy(p => p);
于 2012-04-19T12:12:16.207 回答
0

使用Array.Sort(Array,IComparer)并编写您自己的比较器,在您的 String[] 中查找您的键并比较它们。

于 2012-04-19T12:13:00.057 回答
0

您可以创建IComparer一个使用

public virtual void Sort(
    IComparer comparer
)

http://msdn.microsoft.com/en-us/library/0e743hdt.aspx

于 2012-04-19T12:13:28.030 回答
0

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.

于 2012-04-19T12:15:00.260 回答