对二维数组进行排序
我有一个二维数组,我想根据第一列的内容按降序排序,但是我希望数组保留每一行并在第一列移动时移动第二列。举个例子;
[2, 5]
[4, 18]
[1, 7]
[9, 3]
将被分类为:
[9, 3]
[4, 18]
[2, 5]
[1, 7]
谢谢。
I've got a 2D array that I'd like to sort into descending order depending on the contents of the first column, however I'd like the array to retain each row and move the second column as the first moves. To put it into an example;
[2, 5]
[4, 18]
[1, 7]
[9, 3]
would be sorted into:
[9, 3]
[4, 18]
[2, 5]
[1, 7]
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
试试这个:
我还没有测试过这个,但它应该可以工作。请注意,您可能需要反转减法以更改降序。
Try this:
I haven't tested this but it should work. Note you may want to reverse the subtraction to change descending.
它只不过是基数排序。它的C代码如下:
这里它对数组中的数字进行操作。编辑代码来获取上述问题的代码并不困难。这里数组的每个元素都被视为该 ROW NUMBER 的数字。
It's nothing but Radix Sort. Its C code is as follows:
Here it's operating on digits of the numbers in array. It's not much harder to edit the code to get code for the above problem. Here each element of the array is considered as a digit for that ROW NUMBER.
我不能具体谈论java,但算法应该是可翻译的。要点是在交换时移动该行的两个(或多个)元素。
I can't speak to java specifically but the algorithm should be translatable. The point is to move both elements (or more) of the row when swapping.