简体   繁体   English

按照 C# 中第一列的排序算法对第二列的二维数组进行排序

[英]sorting 2D array with second column following the sorting algorithm of the first in C#

I have a [2,5000] array of strings that I want to sort just based on the first row, but I want second row to follow that sorting algorithm.我有一个 [2,5000] 字符串数组,我只想根据第一行对其进行排序,但我希望第二行遵循该排序算法。

Can I get a lead idea on how I should be approaching this situation?我能否就我应该如何处理这种情况获得主要想法?

Edit: My array looks like编辑:我的阵列看起来像

{15, 13, 16, 19, 25, 29, 11} {wed 12, thu 13, fri 14, sat 15, sun 16, mon 17} {15, 13, 16, 19, 25, 29, 11} {周三 12,周四 13,周五 14,周六 15,周日 16,周一 17}

and I want it as我想要它作为

{29, 25, 19, 16, 15, 13, 11} {sun 16, sat 15, fri 14, wed 12, thu 13, mon 17} {29, 25, 19, 16, 15, 13, 11} {sun 16, sat 15, fri 14, wed 12, thu 13, mon 17}

Don't create a two dimensional array;不要创建二维数组; create a single dimensional array of some class that has two properties, and then sort that based on a custom comparer that only compares one of the values.创建一些类,它具有两个属性的一维阵列,然后排序基于自定义比较,只有比较值中的一个。

I suppose this is an assignment.我想这是一个任务。 You need to code a sorting algorithm (there are many, including some very simple ones ), and while sorting the first row, perform all the swaps on both the first and second row.您需要编写一个排序算法(有很多,包括一些非常简单的算法),并在对第一行进行排序时,对第一行和第二行执行所有交换。

ie a sorting algorithm might look like即排序算法可能看起来像

void Sort(string[] array) {
    // some logic to loop over the array, determine which elements to swap
        swap(array[x], array[y]);
}

basically you want to code something like基本上你想编码类似的东西

void Sort(string[] firstRow, string[] secondRow) {
    // some logic to loop over firstRow, determine which elements to swap
        swap(firstRow[x], firstRow[y]);
        swap(secondRow[x], secondRow[y]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM