如何从多个数据类型的数组列表中对整数数据进行排序?

时间:2017-02-18 08:52:33

标签: java android sorting

大家好我需要了解排序,假设我有一个包含多种数据类型,String和int的自定义数组列表,我想根据价格(int)对数据进行排序,从低到高或从高到低,有谁知道我该如何处理? (请注意,我不是在询问简单的数组列表,我在询问自定义数组列表)

谢谢,这是我的代码:

  public static List<Data> data;
        String[] names= {"a","b","c","d",
                "e","f","g","h","i",
                "j","k","l","m","n",
                "o","p", "q","r","s",
                "t", "u","v","w","x","y","z"};

        Integer[] prices = {900000,450000,2100000,3200000,
                900000,2800000,1400000,800000,
                540000, 700000,650000,1000000,1300000,
                800000, 500000,1200000,900000,1500000,
                558000, 658000,400000,570000,900000,800000,400000};
        }

        private void loadData() {
            for (int i =0;i< 25;i++){
                data.add(new Data(names[i],prices[i]));
            }
            adapter.notifyDataSetChanged();
        }

我做的逻辑:

Collections.sort(data, new Comparator<Data>() {
            @Override
            public int compare(Data o1, Data o2) {
                int returnVal = 0;
                if (o1.getPrice() < o2.getPrice()) {
                    String d = String.valueOf(o1.getPrice() < o2.getPrice());
                    returnVal = -1;
                    Toast.makeText(getContext(), "data" + String.valueOf(d), Toast.LENGTH_SHORT).show();
                } else if (o1.getPrice() > o2.getPrice()) {
                    returnVal = 1;
                } else if (o1.getPrice() == o2.getPrice()) {
                    returnVal = 0;
                }
                return returnVal;
            } });

2 个答案:

答案 0 :(得分:0)

如果您的名称[]没有重复值,则应使用TreeMap来存储此类数据。它将以排序的方式存储数据

这将声明您的数据。

Map<String,Integer> treemap=new TreeMap<String,Integer>();  

向地图添加元素

treemap.put("a", 900000);
treemap.put("b", 450000);

打印排序数据:

for (Map.Entry<K, V> entry : treemap.entrySet()) {
            System.out.println("Key : " + entry.getKey() + " Value : " + entry.getValue());
}

答案 1 :(得分:0)

class Data {

private String names;
private Integer prices;

public Data(String names, Integer prices) {
    this.names = names;
    this.prices = prices;
}

public String getNames() {
    return names;
}

public void setNames(String names) {
    this.names = names;
}

public Integer getPrices() {
    return prices;
}

public void setPrices(Integer prices) {
    this.prices = prices;
}

@Override
public String toString() {
    return "Data{" +
            "names='" + names + '\'' +
            ", prices=" + prices +
            '}';
  }
}
class CustomComparator implements Comparator<Data> {

@Override
public int compare(Data o1, Data o2) {
    return o1.getPrices().compareTo(o2.getPrices());
  }
}
public static List<Data> data;

String[] names= {"a","b","c","d",
        "e","f","g","h","i",
        "j","k","l","m","n",
        "o","p", "q","r","s",
        "t", "u","v","w","x","y","z"};

Integer[] prices = {900000,450000,2100000,3200000,
        900000,2800000,1400000,800000,
        540000, 700000,650000,1000000,1300000,
        800000, 500000,1200000,900000,1500000,
        558000, 658000,400000,570000,900000,800000,400000,70000};

data=new ArrayList<>();

loadData();

Collections.sort(data, new CustomComparator());

将以升序价格提供输出,如果存在价格冲突,它将按时间顺序输出(仅限这些价格)