我试图按正确的顺序对数组进行排序.我已存储的3个变量(1 int,1 String,1 float)在阵列的一个字段.我试图使用本机排序方法,但我的输出没有正确排序:
[1 ,Agavendicksaft ,0.180
, 1 ,Agavendicksaft ,0.284
, 100 ,Röstzwiebel ,0.057
, 103 ,Salz fein ,6.220
, 103 ,Salz fein ,6.452
, 104 ,Salz grob ,0.490
, 114 ,Sesam ,0.735
, 114 ,Sesam ,1.742
, 115 ,Soja Granulat ,43.788
, 116 ,Sonnenblumenkerne ,0.267
, 116 ,Sonnenblumenkerne ,3.636
, 12 ,BAS hell ,0.975
, 12 ,BAS hell ,6.996
, 139 ,Vanille Aroma ,0.068
, 140 ,Weizenmehl Type W1600 ,1.163
, 140 ,Weizenmehl Type W1600 ,1.927
, 141 ,Weizenmehl Type W700 ,138.127
, 141 ,Weizenmehl Type W700 ,45.158
, 142 ,Walnüsse ,0.228
, 144 ,Wiechert Glutenfei ,1.160
, 145 ,Wienerwurst Stange ,0.100
, 150 ,Zitronen Aroma ,0.068
, 151 ,Zucker Normalkristall ,1.039
, 153 ,Wasser ,167.202
, 21 ,Dinkel Flocken ,0.347
, 24 ,Eier ganz ,0.453
, 26 ,Eigelb ProOvo ,0.365
, 29 ,Fenchel ganz ,0.105
, 36 ,Hafer ganz ,3.078
, 47 ,Hirse ganz ,0.133
, 49 ,Honig ,0.186]
Run Code Online (Sandbox Code Playgroud)
所以我有两个问题:
这是我的代码:
s = Results2String(resultSet);
splitResult = s.split("/");
System.out.println(s);
s = null;
s = Results2String(resultSet2);
splitResult2 = s.split("/");
System.out.println(s);
s = null;
s = Results2String(resultSet3);
splitResult3 = s.split("/");
System.out.println(s);
System.out.println(splitResult3[0]);
preResult = new String[splitResult.length + splitResult2.length];
System.arraycopy(splitResult, 0, preResult, 0, splitResult.length);
System.arraycopy(splitResult2, 0, preResult, splitResult.length, splitResult2.length);
System.out.println(Arrays.toString(preResult));
result = new String[splitResult.length + splitResult2.length + splitResult3.length];
System.arraycopy(preResult, 0, result, 0, preResult.length);
System.arraycopy(splitResult3, 0, result, preResult.length, splitResult3.length);
Arrays.sort(result);
System.out.println(Arrays.toString(result));
Run Code Online (Sandbox Code Playgroud)
我假设每个中的一个int,String并且float属于一起.
所以你应该将它们放在一个类中并实现Comparable(文档):
public class Food implements Comparable<Food> {
private int id;
private String name;
private float score; // or maybe kcal... ?
public Food(int id, String name, float score) {
this.id = id;
this.name = name;
this.score = score;
}
@Override
public int compareTo(Food other) {
return id - other.getId();
// use the following if you want to sort by name instead
// return name.compareTo(other.getName());
}
public String getId() {
return id;
}
public String getName() {
return name;
}
}
Run Code Online (Sandbox Code Playgroud)
然后你可以对一组数组进行排序Food....
我仍然不确定你resultSet的是什么(为什么你有三个)但我认为它们是数据库查询的ResultSet.此外,我假设id- name- score组合实际上是数据库中的行.
然后,您可以浏览这些ResultSets并将每行的值放在一个Food对象中:
ResultSet resultSet; // filled somewhere else
List<Food> food = new ArrayList<Food>();
while (resultSet.next()) {
int id = resultSet.getInt(0); // assuming the ids are in the first column
String name = resultSet.getString(1); // assuming the names are in the second column
float score = resultSet.getFloat(2); // assuming the scores are in the third column
food.add(new Food(id, name, score));
}
Collections.sort(food);
Run Code Online (Sandbox Code Playgroud)
我希望这有帮助...
但是,如果您的resultSets 实际上是来自数据库查询的ResultSet,那么您应该在SQL查询中对值进行排序ORDER BY...
| 归档时间: |
|
| 查看次数: |
192 次 |
| 最近记录: |