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

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

sort array of objects java with different properties

问题

我有一个对象数组每个对象包含两个字段
我需要编写一个方法将数组按照第一个字段进行排序
我已经有一个从每个对象中提取第一个字段的方法

每次调用排序方法时我都会收到错误消息

这是我的代码

```java
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) {
            // 交换:
            String tempo = code[i];
            code[i] = code[indMin];
            code[indMin] = tempo;

            // 交换:
            String temp = nom[i];
            nom[i] = nom[indMin];
            nom[indMin] = temp;
        }
    }
}

调用方式:

Classe.trier(tableau, tableau, nbObj);

我还尝试过:

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

但我仍然遇到编译错误。

非常感谢您提前的帮助!


<details>
<summary>英文:</summary>

I have an object array containing two fields per object.
I have to write a method that will sort my array by the first field.
I already have a method which extracts the first field from each object

I always get an error message when I call my method to sort.

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;

        // permutation :
        String temp = nom[i];
        nom[i] = nom[indMin];
        nom[indMin] = temp;
    }
}

}



and the call : 

    Classe.trier(tableau, tableau, nbObj);


I also tried `Class.sort(array.getCode(), array.getName(), nbStudent);`

But I still have compilation errors

thank you in advance for your help




</details>


# 答案1
**得分**: 3

首先,你不需要使用两个单独的数组来包含你的数据。你可以将所有数据放入一个单一的数组中,但更好的方式是使用Java集合。最佳选择是[ArrayList][1]。但是,你最好将两个字段合并到一个对象中。你可以这样做:

```java
public class MyObject {
    String code;
    String nom;

    MyObject(String code, String nom) {
        this.code = code;
        this.nom = nom;
    }
}

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

public static void sort1(ArrayList<MyObject> list) {
    list.sort((obj1, obj2) -> obj1.nom.compareTo(obj2.nom));
}

或者

public static void sort2(ArrayList<MyObject> list) {
    list.sort(Comparator.comparing(MyObject::getNom));
} // 但是,为此你需要在MyObject中添加一个getNom方法

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

MyObject a = new MyObject("abc", "abide");
MyObject b = new MyObject("cab", "whatever you want");

ArrayList<MyObject> list = new ArrayList<>();
list.add(a);
list.add(b);
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:

public class MyObject {
    String code;
    String nom;

    MyObject(String code, String nom) {
        this.code = code;
        this.nom = nom;
    }
}

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:

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

Or

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

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

MyObject a = new MyObject(&quot;abc&quot;, &quot;abide&quot;);
MyObject b = new MyObject(&quot;cab&quot;, &quot;whatever you want&quot;);

ArrayList&lt;MyObject&gt; list = new ArrayList&lt;&gt;();
list.add(a);
list.add(b);
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:

确定