按不同属性对Java中的对象数组进行排序

huangapple go评论98阅读模式
英文:

sort array of objects java with different properties

问题

  1. 我有一个对象数组每个对象包含两个字段
  2. 我需要编写一个方法将数组按照第一个字段进行排序
  3. 我已经有一个从每个对象中提取第一个字段的方法
  4. 每次调用排序方法时我都会收到错误消息
  5. 这是我的代码
  6. ```java
  7. public static void trier(String[] code, String[] nom, int nbObj) {
  8. for (int i = 0; i < nbObj - 1; i++) {
  9. int indMin = i;
  10. for (int j = i + 1; j < nbObj; j++)
  11. if (code[j].compareTo(code[indMin]) < 0)
  12. indMin = j;
  13. if (indMin != i) {
  14. // 交换:
  15. String tempo = code[i];
  16. code[i] = code[indMin];
  17. code[indMin] = tempo;
  18. // 交换:
  19. String temp = nom[i];
  20. nom[i] = nom[indMin];
  21. nom[indMin] = temp;
  22. }
  23. }
  24. }

调用方式:

  1. Classe.trier(tableau, tableau, nbObj);

我还尝试过:

  1. Classe.sort(array.getCode(), array.getName(), nbStudent);

但我仍然遇到编译错误。

非常感谢您提前的帮助!

  1. <details>
  2. <summary>英文:</summary>
  3. I have an object array containing two fields per object.
  4. I have to write a method that will sort my array by the first field.
  5. I already have a method which extracts the first field from each object
  6. I always get an error message when I call my method to sort.
  7. Here is my code:

public static void trier(String[]code, String[]nom, int nbObj) {
for(int i = 0; i < nbObj-1; i++) {
int indMin = i;
for (int j = i+1; j < nbObj; j++)
if (code[j].compareTo(code[indMin]) < 0)
indMin = j;
if (indMin != i) {
// permutation :
String tempo = code[i];
code[i] = code[indMin];
code[indMin] = tempo;

  1. // permutation :
  2. String temp = nom[i];
  3. nom[i] = nom[indMin];
  4. nom[indMin] = temp;
  5. }
  6. }

}

  1. and the call :
  2. Classe.trier(tableau, tableau, nbObj);
  3. I also tried `Class.sort(array.getCode(), array.getName(), nbStudent);`
  4. But I still have compilation errors
  5. thank you in advance for your help
  6. </details>
  7. # 答案1
  8. **得分**: 3
  9. 首先,你不需要使用两个单独的数组来包含你的数据。你可以将所有数据放入一个单一的数组中,但更好的方式是使用Java集合。最佳选择是[ArrayList][1]。但是,你最好将两个字段合并到一个对象中。你可以这样做:
  10. ```java
  11. public class MyObject {
  12. String code;
  13. String nom;
  14. MyObject(String code, String nom) {
  15. this.code = code;
  16. this.nom = nom;
  17. }
  18. }

现在你有一个包含2个字段的类。你的目标是按照它们的第二个字段(nom)对这些对象的集合进行排序。自Java 8以来,你可以轻松实现这一点:

  1. public static void sort1(ArrayList<MyObject> list) {
  2. list.sort((obj1, obj2) -> obj1.nom.compareTo(obj2.nom));
  3. }
  4. 或者
  5. public static void sort2(ArrayList<MyObject> list) {
  6. list.sort(Comparator.comparing(MyObject::getNom));
  7. } // 但是,为此你需要在MyObject中添加一个getNom方法

记得将你的对象正确放入集合中。例如:

  1. MyObject a = new MyObject("abc", "abide");
  2. MyObject b = new MyObject("cab", "whatever you want");
  3. ArrayList<MyObject> list = new ArrayList<>();
  4. list.add(a);
  5. list.add(b);
  6. triage(list);
英文:

First of all, you don't have to use 2 separate arrays to contain your data. You can put everything in a single array, but better way is to use Java Collections. Perfect choice is ArrayList. However, you still better combine two fields into a single object. You can do it like this:

  1. public class MyObject {
  2. String code;
  3. String nom;
  4. MyObject(String code, String nom) {
  5. this.code = code;
  6. this.nom = nom;
  7. }
  8. }

Now you have a class containing 2 fields. Your aim is to sort a collection of such objects by their second field (nom). You can do this easily since Java 8:

  1. public static void sort1(ArrayList&lt;MyObject&gt; list) {
  2. list.sort((obj1, obj2) -&gt; obj1.nom.compareTo(obj2.nom));
  3. }

Or

  1. public static void sort2(ArrayList&lt;MyObject&gt; list) {
  2. list.sort(Comparator.comparing(MyObject::getNom));
  3. } // However for this you need to add method getNom to MyObject

Remember to put your objects in the collection properly.
For example:

  1. MyObject a = new MyObject(&quot;abc&quot;, &quot;abide&quot;);
  2. MyObject b = new MyObject(&quot;cab&quot;, &quot;whatever you want&quot;);
  3. ArrayList&lt;MyObject&gt; list = new ArrayList&lt;&gt;();
  4. list.add(a);
  5. list.add(b);
  6. trier(list);

huangapple
  • 本文由 发表于 2020年8月20日 20:48:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/63505457.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定