gpt4 book ai didi

基于多个参数的Java数组排序

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:59:17 24 4
gpt4 key购买 nike

我有一个数组,我想按升序对其进行排序。但是,我想引用 boolean 数组对它们进行排序。我想按升序对 true 的值进行排序,然后对 false 的值进行排序升序。很少有人纠结于如何到达那里。

这是我目前拥有的:

Object[] arr = new Object[6];

arr[0] = new Object(2);
arr[1] = new Object(5);
arr[2] = new Object(3);
arr[3] = new Object(1);
arr[4] = new Object(6);
arr[5] = new Object(4);

Available[] avalarr = new Available[6];

availarr[0] = new Available (true);
availarr[1] = new Available (false);
availarr[2] = new Available (false);
availarr[3] = new Available (true);
availarr[4] = new Available (true);
availarr[5] = new Available (false);

我需要的输出是:

1 2 6 3 4 5

最佳答案

代码:

import java.util.Arrays;

public class SelectiveSort {

public static void main(String[] args) {

Item [] items = new Item [6];
items[0] = new Item(2, true);
items[1] = new Item(5, false);
items[2] = new Item(3, false);
items[3] = new Item(1, true);
items[4] = new Item(6, true);
items[5] = new Item(4, false);

System.out.println("Before Sorting:");
// Removed enhanced for loop
for(int i = 0; i < items.length; i++) {
System.out.print(items[i].getIntValue() + " ");
}

// Sorting
Arrays.sort(items);

System.out.println("\n\nAfter Sorting:");
// Removed enhanced for loop
for(int i = 0; i < items.length; i++) {
System.out.print(items[i].getIntValue() + " ");
}
System.out.println();
}
}

class Item implements Comparable<Item> {

private int _intValue;
private boolean _boolValue;

public Item(int intValue, boolean boolValue) {
_intValue = intValue;
_boolValue = boolValue;
}

public int getIntValue() { return _intValue; }

public boolean getBoolValue() { return _boolValue; }

@Override
public int compareTo(Item otherItem) {

// Using explicit comparison
int boolComparison = (_boolValue == otherItem._boolValue) ? 0 :
(_boolValue) ? 1 : -1;
return (boolComparison != 0) ? -boolComparison :
( (_intValue == otherItem.getIntValue()) ? 0 :
(_intValue > otherItem.getIntValue()) ? 1 : -1);
}
}

输出:

排序前:2 5 3 1 6 4

排序后:1 2 6 3 4 5


解释:

想法是让您的“Item”实现Comparable,并根据所需的顺序覆盖compareTo(Item otherItem) 函数。
完成后,您需要做的就是对 Item 数组调用 Arrays.sort()

版本 2(没有 Comparable/Comparator):

public class SelectiveSort {

public static void main(String[] args) {

Item [] items = new Item [6];
items[0] = new Item(2, true);
items[1] = new Item(5, false);
items[2] = new Item(3, false);
items[3] = new Item(1, true);
items[4] = new Item(6, true);
items[5] = new Item(4, false);

System.out.println("Before Sorting:");
for(int i = 0; i < items.length; i++) {
System.out.print(items[i].getIntValue() + " ");
}

// Sorting
bubbleSort(items);

System.out.println("\n\nAfter Sorting:");
for(int i = 0; i < items.length; i++) {
System.out.print(items[i].getIntValue() + " ");
}
System.out.println();
}

public static void bubbleSort(Item [] items) {
int n = items.length;
do {
int newN = 0;
for(int i = 1; i < n; i++) {
if(compareTo(items[i-1], items[i]) == 1) {
Item temp = items[i-1];
items[i-1] = items[i];
items[i] = temp;

newN = i;
}
}
n = newN;
} while (n != 0);
}

public static int compareTo(Item item1, Item item2) {

int boolComparison = (item1.getBoolValue() == item2.getBoolValue())
? 0 : (item1.getBoolValue()) ? 1 : -1;
return (boolComparison != 0) ? -boolComparison :
( (item1.getIntValue() == item2.getIntValue()) ? 0 :
(item1.getIntValue() > item2.getIntValue()) ? 1 : -1);
}
}

关于基于多个参数的Java数组排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30585329/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com