对2D数组按2列排序



我正在寻找一种有效的方法来对2D数组中的数据进行排序。数组可以有很多行和列,但是在这个例子中,我将把它限制为6行和5列。数据是字符串,有些是单词。我只包括下面的一个单词,但在实际数据中有几列单词。我意识到如果我们排序,我们应该把数据当作数字?

string[,] WeatherDataArray = new string[6,5];

数据是每天读取并记录的一组天气数据。这些数据要经过他们系统的许多部分,我无法改变这些部分,它们以一种需要排序的方式到达我这里。一个示例布局可以是:

Day number, temperature, rainfall, wind, cloud

数据矩阵可以像这样

3,20,0,12,cumulus
1,20,0,11,none
23,15,0,8,none
4,12,0,1,cirrus
12,20,0,12,cumulus
9,15,2,11,none

他们现在希望对数据进行排序,以便温度降序排列,天数升序排列。结果将是

1,20,0,11,none
3,20,0,12,cumulus
12,20,0,12,cumulus
9,15,2,11,none
23,15,0,0,none
4,12,0,1,cirrus

数组被存储,稍后他们可以将其提取到表中并对其进行大量分析。提取端没有改变,所以我不能对表中的数据进行排序,我必须以正确的格式创建数据,以匹配他们现有的规则。

我可以解析数组的每一行并对它们进行排序,但这似乎是一个非常冗长的方法。必须有一个更快更有效的方法来排序这个二维数组的两列?我想我可以把它发送给一个函数并返回排序数组,如:

private string[,]  SortData(string[,] Data)
{
//In here we do the sorting
}

有什么想法吗?

我同意另一个答案,最好将数据的每一行解析为封装数据的类的实例,从该数据创建新的1D数组或列表。然后你将对1D集合进行排序,并将其转换回2D数组。

然而,另一种方法是编写一个IComparer类,您可以使用它来比较二维数组中的两行,如下所示:

public sealed class WeatherComparer: IComparer
{
readonly string[,] _data;
public WeatherComparer(string[,] data)
{
_data = data;
}
public int Compare(object? x, object? y)
{
int row1 = (int)x;
int row2 = (int)y;
double temperature1 = double.Parse(_data[row1, 1]);
double temperature2 = double.Parse(_data[row2, 1]);
if (temperature1 < temperature2)
return 1;
if (temperature2 < temperature1)
return -1;
int day1 = int.Parse(_data[row1,0]);
int day2 = int.Parse(_data[row2,0]);
return day1.CompareTo(day2);
}
}

请注意,这包括对要排序的2D数组的引用,并在必要时解析元素以进行排序。

然后你需要创建一个1D数组的索引,这是你实际要排序的。(你不能对2D数组进行排序,但你可以对引用2D数组行索引的1D数组进行排序。)

public static string[,] SortData(string[,] data)
{
int[] indexer = Enumerable.Range(0, data.GetLength(0)).ToArray();
var comparer = new WeatherComparer(data);
Array.Sort(indexer, comparer);
string[,] result = new string[data.GetLength(0), data.GetLength(1)];
for (int row = 0; row < indexer.Length; ++row)
{
int dest = indexer[row];
for (int col = 0; col < data.GetLength(1); ++col)
result[dest, col] = data[row, col];
}
return result;
}

然后可以调用SortData对数据进行排序:

public static void Main()
{
string[,] weatherDataArray = new string[6, 5]
{
{ "3",  "20", "0", "12", "cumulus" },
{ "1",  "20", "0", "11", "none" },
{ "23", "15", "0", "8",  "none" },
{ "4",  "12", "0", "1",  "cirrus" },
{ "12", "20", "0", "12", "cumulus" },
{ "9",  "15", "2", "11", "none" }
};
var sortedWeatherData = SortData(weatherDataArray);
for (int i = 0; i < sortedWeatherData.GetLength(0); ++i)
{
for (int j = 0; j < sortedWeatherData.GetLength(1); ++j)
Console.Write(sortedWeatherData[i,j] + ", ");

Console.WriteLine();
}
}

输出:

1, 20, 0, 11, none,
3, 20, 0, 12, cumulus,
12, 20, 0, 12, cumulus,
9, 15, 2, 11, none,
23, 15, 0, 8, none,
4, 12, 0, 1, cirrus,

请注意,这段代码不包含任何错误检查——它假设数据中没有空,并且所有解析的数据实际上都是可解析的。您可能需要添加适当的错误处理。

在。net Fiddle上试试:https://dotnetfiddle.net/mwXyMs

我建议将数据解析为可以用常规方法排序的对象。比如使用LINQ:

myObjects.OrderBy(obj => obj.Property1)
.ThenBy(obj=> obj.Property2);

将数据视为字符串表只会使处理更加困难,因为在每一步都需要解析值,处理潜在的错误,因为字符串可能为空或包含无效值等。更好的设计是,在读取数据时,只对进行一次解析和错误处理,并在将其写入磁盘或移交给下一个系统时再次将其转换为文本形式。

如果这是一个遗留系统,有很多部件以文本形式处理数据,我仍然会认为首先解析数据,并在一个单独的模块中进行,以便可以重用。这应该允许其他部分被逐部分重写以使用对象格式。

如果这是完全不可行的,你要么需要将数据转换为锯齿数组,即string[][]。或者编写自己的排序,可以交换多维数组中的行。

尝试做出比公认答案更好的东西很有趣,我想我做到了。

更好的理由:

  • 使用哪些列进行排序以及按升序还是降序进行排序不是硬编码的,而是作为参数传递的。在这篇文章中,我了解到他们将来可能会改变他们对如何排序数据的想法。
  • 它支持按不包含数字的列排序,如果他们想按名称列排序。
  • 在我的测试中,对于大数据,它更快,分配更少的内存。

更快的原因:

  • 它从不解析Data的相同索引两次。它缓存数字。
  • 复制时,使用Span.CopyTo代替索引。
  • 它不创建一个新的数据数组,它对行进行排序。这也意味着它不会复制已经在正确位置的行。

用法如下:

DataSorter.SortDataWithSortAguments(array, (1, false), (0, true));

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
namespace YourNamespace;
public static class DataSorter
{
public static void SortDataWithSortAguments(string[,] Data, params (int columnIndex, bool ascending)[] sortingParams)
{
if (sortingParams.Length == 0)
{
return;
// maybe throw an exception instead? depends on what you want
}
if (sortingParams.Length > 1)
{
var duplicateColumns =
from sortingParam in sortingParams
group false by sortingParam.columnIndex
into sortingGroup
where sortingGroup.Skip(1).Any()
select sortingGroup.Key;
var duplicateColumnsArray = duplicateColumns.ToArray();
if (duplicateColumnsArray.Length > 0)
{
throw new ArgumentException($"Cannot sort by the same column twice. Duplicate columns are: {string.Join(", ", duplicateColumnsArray)}");
}
}
for (int i = 0; i < sortingParams.Length; i++)
{
int col = sortingParams[i].columnIndex;
if (col < 0 || col >= Data.GetLength(1))
{
throw new ArgumentOutOfRangeException($"Column index {col} is not within range 0 to {Data.GetLength(1)}");
}
}
int[] linearRowIndeces = new int[Data.GetLength(0)];
for (int i = 0; i < linearRowIndeces.Length; i++)
{
linearRowIndeces[i] = i;
}
Span<int> sortedRows = SortIndecesByParams(Data, sortingParams, linearRowIndeces);
SortDataRowsByIndecesInPlace(Data, sortedRows);
}

private static float[]? GetColumnAsNumbersOrNull(string[,] Data, int columnIndex)
{
if (!float.TryParse(Data[0, columnIndex], out float firstNumber))
{
return null;
}
// if the first row of the given column is a number, assume all rows of the column should be numbers as well
float[] column = new float[Data.GetLength(0)];
column[0] = firstNumber;
for (int row = 1; row < column.Length; row++)
{
if (!float.TryParse(Data[row, columnIndex], out column[row]))
{
throw new ArgumentException(
$"Rows 0 to {row - 1} of column {columnIndex} contained numbers, but row {row} doesn't");
}
}
return column;
}
private static Span<int> SortIndecesByParams(
string[,] Data,
ReadOnlySpan<(int columnIndex, bool ascending)> sortingParams,
IEnumerable<int> linearRowIndeces)
{
var (firstColumnIndex, firstAscending) = sortingParams[0];
var firstColumn = GetColumnAsNumbersOrNull(Data, firstColumnIndex);
IOrderedEnumerable<int> sortedRowIndeces = (firstColumn, firstAscending) switch
{
(null, true) => linearRowIndeces.OrderBy(row => Data[row, firstColumnIndex]),
(null, false) => linearRowIndeces.OrderByDescending(row => Data[row, firstColumnIndex]),
(not null, true) => linearRowIndeces.OrderBy(row => firstColumn[row]),
(not null, false) => linearRowIndeces.OrderByDescending(row => firstColumn[row])
};
for (int i = 1; i < sortingParams.Length; i++)
{
var (columnIndex, ascending) = sortingParams[i];
var column = GetColumnAsNumbersOrNull(Data, columnIndex);
sortedRowIndeces = (column, ascending) switch
{
(null, true) => sortedRowIndeces.ThenBy(row => Data[row, columnIndex]),
(null, false) => sortedRowIndeces.ThenByDescending(row => Data[row, columnIndex]),
(not null, true) => sortedRowIndeces.ThenBy(row => column[row]),
(not null, false) => sortedRowIndeces.ThenByDescending(row => column[row])
};
}
return sortedRowIndeces.ToArray();
}
private static void SortDataRowsByIndecesInPlace(string[,] Data, Span<int> sortedRows)
{
Span<string> tempRow = new string[Data.GetLength(1)];
for (int i = 0; i < sortedRows.Length; i++)
{
while (i != sortedRows[i])
{
Span<string> firstRow = MemoryMarshal.CreateSpan(ref Data[i, 0], tempRow.Length);
Span<string> secondRow = MemoryMarshal.CreateSpan(ref Data[sortedRows[i], 0], tempRow.Length);
firstRow.CopyTo(tempRow);
secondRow.CopyTo(firstRow);
tempRow.CopyTo(secondRow);
(sortedRows[i], sortedRows[sortedRows[i]]) = (sortedRows[sortedRows[i]], sortedRows[i]);
}
}
}
}

PS:考虑到我的责任,我不应该花这么多时间在这上面,但这很有趣。

最新更新