从最高到最低顺序和位置从另一个数组列表更改 java

order highest to lowest and position changes from another arraylist java

我有两个arraylist.,

一个是整数数组,另一个是字符串数组。在这里,我需要对整数数组值进行排序,以从最高值到最低值排序。这时候我也需要从字符串数组中移动位置。

例如:

字符串数组:[注册和行政主管、网页设计、IT - 软件工程师、会计师、网络工程师] 整数数组:[4, 2, 2, 6, 2]

但是我需要上面的结果.,

字符串数组:[会计、注册和行政主管、IT - 软件工程师、网络工程师、网页设计] 整数数组:[6, 4, 2, 2, 2]

需要对整数数组进行排序,同时更改字符串数组的位置,如果整数数组中出现相同的值,则需要按字母顺序排序。我该怎么办?

有最短的路吗?

在你的情况下,数组小意味着使用冒泡排序(冒泡排序对于大数组来说不是高性能)。根据位置对 int 数组进行排序你交换字符串数组也像这样

            int intArray[] = new int[]{4, 2, 2, 6, 2};
            String strArray[]=new String[]{Accountant, Register & Head of Administration, IT - Software Engineer, Network Engineer,Web Designing}
            int n = intArray.length;
            int temp = 0,temp1=0;               
            for(int i=0; i < n; i++){
                    for(int j=1; j < (n-i); j++){                               
                            if(intArray[j-1] < intArray[j]){                                        
                                    temp = intArray[j-1];
                                    temp1=strArray[j-1];
                                    intArray[j-1] = intArray[j];
                                    strArray[j-1] = strArray[j];
                                    intArray[j] = temp;
                                    strArray[j] = temp1;
                            }                               
                    }
            }
List<Pair<String, Integer>> data= new ArrayList<Pair<String, Integer>();
data.add(new Pair("Register & Head of Administration", 4));
...
data.add(new Pair("Network Engineer", 2));

Collections.sort(data, new Comparator<Pair<String, Integer>>() {
@Override
public int compare(final Pair<String, Integer> o1, final Pair<String, Integer> o2) {
    // TODO: implement your logic here, e.g.
    return o1.second.compareTo(o2.second);
}

尝试这样的事情

这将是您的数据结构

class DataModel{
  int priority;
  String priorityName;
  DataModel(int priority,String priorityName){
       this.priority = priority; 
       this.priorityName = priorityName;
   }
}

现在制作这个 class dataArrayList<DataModel>

的 Arraylist

使用方法按优先级对 int 数组进行排序

for(int i=0; i < n; i++){
   for(int j=1; j < (n-i); j++){

            if(intArray[j-1] < intArray[j]){

             //add the data of your largest int number in your int Array and use its position to get the data of your String array. 
             dataArrayList.add(new DataModel(your_lagest_int, your_department)); 

        }
    }
}

您应该得到一个包含所有数据的排序数组列表。

为什么不使用第三个 class 将所有字符串分组为权重。

创建一个比较器来排序 WeightString 然后使用 Stream API 来使用数据。

public class WeightString {
    public String value;
    public int weight;

    public WeightString(String value, int weight) {
        this.value = value;
        this.weight = weight;
    }
}

public Comparator<WeightString> mWeightStringComparator = 
        (o1, o2) -> Integer.compare(o1.weight, o2.weight);


// in code
WeightString[] weightStrings = new WeightString[]{
        new WeightString("a", 12),
        new WeightString("b", 1),
        new WeightString("c", 8),
        new WeightString("d", 4)
};

Stream<WeightString> stringStream = Arrays.stream(weightStrings);
stringStream.sorted(mWeightStringComparator);
WeightString[] orderedWeightStrings = (WeightString[]) stringStream.toArray();

//orderedWeightStrings : [b,d,c,a]

希望对您有所帮助:

int[] intArray = new int[] { 4, 2, 2, 6, 2 };
String[] strArray = new String[] { "Register & Head of Administration", "Web Designing", "IT - Software Engineer", "Accountant", "Network Engineer" };
int tmp0 = 0;
String tmp1 = "";
for (int i = 0; i < intArray.length; i++) {

    for (int j = i + 1; j < intArray.length; j++) {

        if (intArray[j] > intArray[i]) {

            // swap in int-Array
            tmp0 = intArray[i];
            intArray[i] = intArray[j];
            intArray[j] = tmp0;

            // swap in string-Array
            tmp1 = strArray[i];
            strArray[i] = strArray[j];
            strArray[j] = tmp1;
        } else if (intArray[j] == intArray[i]) {

            // sorts alphabetically
            if (strArray[j].compareTo(strArray[i]) < 0) {

                tmp1 = strArray[i];
                strArray[i] = strArray[j];
                strArray[j] = tmp1;
            }
        }
    }
}

//output
for (int k = 0; k < intArray.length; k++) {

    System.out.println(strArray[k] + " " + intArray[k]);
}

输出:

Accountant 6 Register & Head of Administration 4 IT - Software Engineer 2 NetworkEngineer 2 Web Designing 2