简体   繁体   English

在不使用预定义排序方法的情况下用Java对arraylist进行排序

[英]Sorting an arraylist in java without using predefined sort method

I want to sort an arraylist by sales person name but I'm not allowed to use built-in sort. 我想按销售人员姓名对arraylist进行排序,但不允许使用内置排序。
In the followin code, I'm trying to manually sort it but it shows error (required: variable found: value) on the lines commented. 在以下代码中,我尝试手动对其进行排序,但在注释的行上显示错误(必填:找到的变量:值)。

    int j,k;
    boolean flag = true;  
    Salesperson person = new Salesperson(null, 0, 0);

    while (flag) {
        flag = false;
        for (j = 0, k=1; j < salesperson.size()-1; j++, k++) {
            if (salesperson.get(j).getName().compareToIgnoreCase(salesperson.get(j+1).getName()) > 0) {                                             // ascending sort

                person = salesperson.get(j);
             //   salesperson.get(j) = salesperson.get(k);     
             //   salesperson.get(k) = person;
                flag = true;
            }
        }
    }

you can't sort an array in one loop , at least that's what i know , even if you got the part inside the if statement right , the algorithm is still wrong . 您不能在一个循环中对数组进行排序,至少这是我所知道的,即使您将部分放入if语句中,该算法仍然是错误的。 You only compared each person with the adjacent person in one pass only. 您只需一次通过就可以将每个人与相邻的人进行比较。

Let's say you are sorting an array of numbers that looks like this : 假设您要对一组数字进行排序,如下所示:

5 4 6 3 1 2 with this algorithm , you would have this result 4 5 3 1 2 6 5 4 6 3 1 2使用此算法,您将获得此结果4 5 3 1 2 6

on one pass like you did it's still not sorted , only every number got compared to the adjacent one. 像您所做的那样,仍然没有排序,只有每个数字都与相邻的数字进行了比较。

Also about setting a value , use ArrayList.set() like Eran answered . 关于设置值,也可以像Eran Answers一样使用ArrayList.set()

Here are some sorting algorithms 这是一些排序算法

//salesperson.get(j) = salesperson.get(k);     
//   salesperson.get(k) = person;

You are taking out the values through the get() method. 您正在通过get()方法取出这些值。 That can't be used to assign values. 那不能用来赋值。 That is why you get compilation errors. 这就是为什么您会收到编译错误的原因。

What you should do is create a temp object and assigned to that and do the swapping of objects But you must recheck your sorting iteration logic 您应该做的是创建一个临时对象并为其分配对象并进行对象交换,但是您必须重新检查排序迭代逻辑

This is the correct code. 这是正确的代码。 I was making mistake in theses lines 我在这些方面犯了错误

         //salesperson.get(j) = salesperson.get(k);     
         //   salesperson.get(k) = person;

these were replaced by the following lines, and now there is no error 这些被替换为以下几行,现在没有错误

                salesperson.set(j, salesperson.get(k));     
                salesperson.set(k, person);

Thanks for your answers 谢谢你的回答

    int j,k;
    boolean flag = true;  // will determine when the sort is finished
    Salesperson person = new Salesperson(null, 0, 0);

    while (flag) {
        flag = false;
        for (j = 0, k=1; j < salesperson.size()-1; j++, k++) {
            if (salesperson.get(j).getName().compareToIgnoreCase(salesperson.get(j+1).getName()) > 0) {                                             // ascending sort

                person = salesperson.get(j);
                salesperson.set(j, salesperson.get(k));     
                salesperson.set(k, person);
                flag = true;
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM