在JAVA中对两个或多个数组进行排序
我想根据两个数组的数值对它们进行排序。例如,
apple[] = [apples, green, 9180, 16.19];
orange[] = [oranges, orange color, 9180, 25.18];
我想根据数组中的第三个值对其进行排序,即 16.19
我将如何处理这个问题?
注意数组声明仅用于说明目的。
结果必须如下所示:
String[] apples = [apples, green, 9180, 16.19];
String[] oranges = [oranges, orange color, 9180, 25.18];
String[] grapes = [grapes, green, 9180, 35.16]
String[] strawberry = [strawberries, red, 9180, 45.36]
每一行都是一个单个单独的数组。必须按照最后一个数字升序排序即 16.19, 25.18, 35.16
数组有四个字段 [apples, green, 9180, 16.19];苹果:水果的名称;绿色:水果的颜色; 9180:农场密码; 16.19:果实产量。我必须根据水果的产量对它们进行排序。希望我说清楚了。
I would like to sort two arrays according to their numeric values.For eg
apple[] = [apples, green, 9180, 16.19];
orange[] = [oranges, orange color, 9180, 25.18];
I would like to sort it according to the 3rd value in the array i.e 16.19
How will I go about this ?
Note array declaration is for illustrative purposes only.
The result must look like this:
String[] apples = [apples, green, 9180, 16.19];
String[] oranges = [oranges, orange color, 9180, 25.18];
String[] grapes = [grapes, green, 9180, 35.16]
String[] strawberry = [strawberries, red, 9180, 45.36]
Each line is a single separate array. It must be sorted in ascending order according to the last number i.e 16.19, 25.18, 35.16
The array has four fields [apples, green, 9180, 16.19]; Apples: name of the fruit; Green: color of the fruit; 9180: pin code of farm; 16.19: yield of fruits. I must sort these according to the yield of fruits. Hope I am clear.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我允许自己假设您没有四个名为
oranges
、apples
、strawberry
和grapes
的变量。如果这样做,请在排序之前将它们放入数组中。对
Comparable
数组进行排序的示例代码:输出:
如果您想要更通用的东西,您可以定义一个数组比较器,它将比较列作为其构造函数中的参数(请参阅更新示例):
I allowed myself the assumption that you don't have four variables named
oranges
,apples
,strawberries
andgrapes
. If you do, put them inside an array before sorting.Example code to sort an array of arrays of
Comparable
s:Output:
If you want something even more generic, you can define an array comparator which takes the comparison column as a parameter in its constructor (see updated example):
您可以将数组封装在一个类中,该类将实现 可比较的界面。这将使您能够使用您想要的任何比较策略。
You can encapsulate your arrays in a class that will implement the Comparable interface. This will give you the ability to use whatever comparison strategy you want.
解释后更新:
我假设您将这些数组存储在列表或其他数组中。在这种情况下,您可以使用
Collections.sort()
或Arrays.sort()
。由于sort()
只能对元素可相互比较的数组进行排序,因此您必须实现一个Comparator
来比较数组的最后一个元素和将其传递给排序方法。 比较器示例Update after explanation:
I assume you store these arrays in a list or an other array. It this case, you can use
Collections.sort()
orArrays.sort()
. Sincesort()
can sort only an array whose elements are mutually comparable, you have to implement aComparator<Object[]>
that compares the last elements of the arrays and pass it to the sort method. Example on comparator问题是:
为什么您甚至将带有属性的 Items 封装为数组?我更喜欢一个名为“Fruits”的封装类。在此类中,您可以实现 Comparable Interface 或编写单独的 Comparator。
之后,你会得到这样的东西:
Question is:
Why do you even encapsulate your Items with attributes as an array? I would prefer a class for encapsulating, called "Fruits". In this class you can implement the Comparable Interface or write a separate Comparator.
After that, you you'll have something like this:
尝试这个函数
请参阅此处 - 如何对数组进行排序。
try this function
See here - how to sort an array.