如果我有如下的二维数组,是否可以按最后一个元素对数组进行排序? (最后一个元素是指2,10,1,5)
string[,] original = new string[4, 3] { {"apple","price1", "2"}, {"orange","price2", "10"} , {"Pineapple","price5", "1"}, {"Kiwi","price3", "5"}};
答案 0 :(得分:4)
这并没有直接回答这个问题,但更清晰的实现可能就像......
public class Fruit : IComparer<Fruit>, IComparable<Fruit>
{
public Fruit(string name, double price, int quantity)
{
Name = name;
Price = price;
Quantity = quantity;
}
protected int Quantity { get; set; }
protected double Price { get; set; }
protected string Name { get; set; }
public int CompareTo(Fruit other)
{
if (Quantity < other.Quantity) return 1;
if (Quantity > other.Quantity) return -1;
return 0;
}
public override string ToString()
{
return string.Format("{0} {1} {2}", Name, Price, Quantity);
}
public int Compare(Fruit x, Fruit y)
{
if (x.Quantity > y.Quantity) return 1;
if (x.Quantity < y.Quantity) return -1;
return 0;
}
}
然后你可以使用这样的新类...
var fruits = new List<Fruit>
{
new Fruit("Apple", 0.30, 2),
new Fruit("Orange", 0.50, 10),
new Fruit("Pineapple", 0.35, 1),
new Fruit("Kiwi", 0.33, 5)
};
Console.WriteLine("Before sort");
foreach (var fruit in fruits)
{
Console.WriteLine(fruit.ToString());
}
fruits.Sort();
Console.WriteLine("After sort");
foreach (var fruit in fruits)
{
Console.WriteLine(fruit.ToString());
}
fruits.Reverse();
Console.WriteLine("After reverse");
foreach (var fruit in fruits)
{
Console.WriteLine(fruit.ToString());
}
Console.ReadLine();
输出:
答案 1 :(得分:0)
你可以做这样的事情
public static void Sort(string[,] original)
{
var data=new List<List<string>>();
for (int i = 0; i < original.GetLength(0); i++)
{
var m=new List<string>();
for (int j = 0; j < original.GetLength(1); j++)
{
m.Add(original[i,j]) ;
}
data.Add(m);
}
var l = data.OrderBy(x => int.Parse(x[2])).ToList();
for (int i = 0; i < original.GetLength(0); i++)
{
for (int j = 0; j < original.GetLength(1); j++)
{
original[i, j] = l[i][j];
}
}
}
答案 2 :(得分:0)
这是可能的,但正如评论中已经指出的那样,有更好的方法来设计数据结构。无论如何,如果你想坚持你的阵列,可能的(有许多方法进行排序)解决方案可能如下所示:
string[,] original = new string[4, 3]
{
{"apple","price1", "2"},
{"orange","price2", "10"} ,
{"Pineapple","price5", "1"},
{"Kiwi","price3", "5"}
};
// extract the data
var indexAndOrder = new List<Tuple<string, string, int>>();
for (var i = 0; i < original[0,0].Length - 1; i++)
{
var current = int.Parse(original[i, 2]);
indexAndOrder.Add(new Tuple<string, string, int>(original[i,0], original[i,1], current));
}
// anonymous object to sort the indices
var sortedArray = indexAndOrder
.Select (ao => new { Name = ao.Item1, Price = ao.Item2, Index = ao.Item3})
.ToList()
.OrderBy (ao => ao.Index)
//.OrderByDescending (ao => ao.Index)
.ToArray();
foreach (var item in sortedArray)
{
Console.WriteLine(string.Format("{0} - {1} - {2}", item.Index, item.Name, item.Price));
}
输出结果为:
1 - Pineapple - price5
2 - apple - price1
5 - Kiwi - price3
10 - orange - price2
该算法总是可以在函数中提取并返回一个新的排序数组。
答案 3 :(得分:0)
作为参考,也可以使用单个LINQ查询(但可能不是最有效):
string[,] original = new string[4, 3] { { "apple", "price1", "2" }, { "orange", "price2", "10" }, { "Pineapple", "price5", "1" }, { "Kiwi", "price3", "5" } };
var l = original.Cast<string>()
.Select((element, index) => new { element, index })
.GroupBy(x => x.index / 3)
.Select(x => new
{
Id = x.ElementAt(0).element,
Name = x.ElementAt(1).element,
Value = int.Parse(x.ElementAt(2).element)
})
.OrderBy(x => x.Value)
.Select(x => new string[] { x.Id, x.Name, x.Value.ToString() })
.ToArray();
请参阅this question了解详情。
答案 4 :(得分:0)
static int MCompare(string[] x, string[] y)
{
return String.Compare(x[2], y[2]);
}
static void Main(string[] args)
{
string[,] original = new string[4, 3]
{
{"apple","price1", "2"},
{"orange","price2", "10"} ,
{"Pineapple","price5", "1"},
{"Kiwi","price3", "5"}
};
string[][] temp = new string[4][];
for (int i = 0; i < 4; i++)
{
temp[i] = new string[3];
for (int j = 0; j < 3; j++)
{
temp[i][j] = original[i, j];
}
}
Array.Sort<string[]>(temp, MCompare);
}
tmp是排序结果