suh*_*MAN 7 c# arrays sorting list
我有一个字符串数组列表,其中数组格式为[Animal,Breed,Name]:
{ ["Dog", "Golden Retriever", "Rex"],
["Cat", "Tabby", "Boblawblah"],
["Fish", "Clown", "Nemo"],
["Dog", "Pug", "Daisy"],
["Cat", "Siemese", "Wednesday"],
["Fish", "Gold", "Alaska"]
}
Run Code Online (Sandbox Code Playgroud)
我如何对这个列表进行排序,使其按"字母"按字母顺序排列,然后按"品种"排列?即:
{ ["Cat", "Siamese", "Boblawblah"],
["Cat", "Tabby", "Wednesday"],
["Dog", "Golden Retriever", "Rex"],
["Dog", "Pug", "Daisy"],
["Fish", "Clown", "Nemo"],
["Fish", "Gold", "Alaska"]
}
Run Code Online (Sandbox Code Playgroud)
我目前正在尝试:
animalList.Sort((s, t) => String.Compare(s[0], t[0]));
Run Code Online (Sandbox Code Playgroud)
但这并没有正确排序第二列.除了按字母顺序按前两列排序外,我如何在第三列中添加?
Tim*_*ter 10
您可以使用LINQ:
animalList = animalList
.OrderBy(arr => arr[0])
.ThenBy(arr => arr[1])
.ToList();
Run Code Online (Sandbox Code Playgroud)
你的样本:
List<string[]> animalList = new List<String[]>{
new []{"Dog", "Golden Retriever", "Rex"},
new []{"Cat", "Tabby", "Boblawblah"},
new []{"Fish", "Clown", "Nemo"},
new []{"Dog", "Pug", "Daisy"},
new []{"Cat", "Siemese", "Wednesday"},
new []{"Fish", "Gold", "Alaska"}
};
Run Code Online (Sandbox Code Playgroud)
结果:
- [0] {string[3]} string[]
[0] "Cat" string
[1] "Siemese" string
[2] "Wednesday" string
- [1] {string[3]} string[]
[0] "Cat" string
[1] "Tabby" string
[2] "Boblawblah" string
- [2] {string[3]} string[]
[0] "Dog" string
[1] "Golden Retriever" string
[2] "Rex" string
- [3] {string[3]} string[]
[0] "Dog" string
[1] "Pug" string
[2] "Daisy" string
- [4] {string[3]} string[]
[0] "Fish" string
[1] "Clown" string
[2] "Nemo" string
- [5] {string[3]} string[]
[0] "Fish" string
[1] "Gold" string
[2] "Alaska" string
Run Code Online (Sandbox Code Playgroud)
你可以做:
var newList = list.OrderBy(r => r[0])
.ThenBy(r => r[1])
.ThenBy(r => r[2])
.ToList();
Run Code Online (Sandbox Code Playgroud)
这将假设您List
将拥有一个字符串数组元素,其长度至少为 3个项目.这将基于阵列的第一个项目,首先对列表进行排序Animal
,然后Bread
再Name
.
如果您List
的定义为:
List<string[]> list = new List<string[]> { new [] {"Dog", "Golden Retriever", "Rex"},
new [] { "Cat", "Tabby", "Boblawblah"},
new [] {"Fish", "Clown", "Nemo"},
new [] {"Dog", "Pug", "Daisy"},
new [] {"Cat", "Siemese", "Wednesday"},
new [] {"Fish", "Gold", "Alaska"}
};
Run Code Online (Sandbox Code Playgroud)
一个更好的方式来处理这个问题将有自定义类,与Type
,Bread
和Name
财产,然后用这个来代替string[]
您可以定义自己的类:
public class Animal
{
public string Type { get; set; }
public string Bread { get; set; }
public string Name { get; set; }
public Animal(string Type, string Bread, string Name)
{
this.Type = Type;
this.Bread = Bread;
this.Name = Name;
}
}
Run Code Online (Sandbox Code Playgroud)
然后定义你的List<Animal>
喜欢:
List<Animal> animalList = new List<Animal>
{
new Animal("Dog", "Golden Retriever", "Rex"),
new Animal("Cat", "Tabby", "Boblawblah"),
new Animal("Fish", "Clown", "Nemo"),
new Animal("Dog", "Pug", "Daisy"),
new Animal("Cat", "Siemese", "Wednesday"),
new Animal("Fish", "Gold", "Alaska"),
};
Run Code Online (Sandbox Code Playgroud)
稍后您可以获得如下排序列表:
List<Animal> sortedList = animalList
.OrderBy(r => r.Type)
.ThenBy(r => r.Bread)
.ToList();
Run Code Online (Sandbox Code Playgroud)
如果需要,可以实现自己的自定义排序,请参阅:如何在Visual C#中使用IComparable和IComparer接口