簡體   English   中英

Java基於關鍵字排序

[英]Java sort based on keywords

我有車輛清單。 我想根據品牌對這些車輛進行分類。 訂單在另一個數組中定義。

這段代碼使用兩個品牌“ Honda”和“ Kia”的排列很好。 本田在起亞之上,其余部分在本田之上。 是否有可能使大小可變的數組通用。 如果數組是“ Chevrolet”,“ Dodge”,“ Ford”怎么辦?

謝謝

//This sorts Honda first, Kia second, and others after.
final String[] makes = new String[]{"Honda","Kia"};
Collections.sort(vehicles, new Comparator<Vehicle>() {
    @Override
    public int compare(Vehicle o1, Vehicle o2) {
        String makeObj1 = o1.getModel().getMakeName().toLowerCase();
        String makeObj2 = o2.getModel().getMakeName().toLowerCase();
        //honda first
        if (makeObj1.equals(makes[0].toLowerCase())) {
            if (makeObj2.equals(makes[0].toLowerCase())) {
                return 0;//honda = honda
            }
            if (makeObj2.equals(makes[1].toLowerCase())) {
                return -1;//honda > kia
            } else {
                return -1;//honda > others
            }
        }
        //kia first
        if (makeObj1.equals(makes[1].toLowerCase())) {
            if (makeObj2.equals(makes[0].toLowerCase())) {
                return 1;//kia < honda
            }
            if (makeObj2.equals(makes[1].toLowerCase())) {
                return 0;//kia = kia
            } else {
                return -1;//kia > others
            }
        }
        //honda second
        if (makeObj2.equals(makes[0].toLowerCase())) {
            if (makeObj1.equals(makes[1].toLowerCase())) {
                return 1;//kia < honda
            } else {
                return 1;//other < honda
            }
        }
        //kia second
        if (makeObj2.equals(makes[1].toLowerCase())) {
            return 1;//others < kia
        }
        return 0;//all cases should been covered
    }
});

沒有測試,但是您可以嘗試:

public int compare(Vehicle o1, Vehicle o2) {

    String makeObj1 = o1.getModel().getMakeName().toLowerCase();
    String makeObj2 = o2.getModel().getMakeName().toLowerCase();

    int indexMake1 = Arrays.asList(makes).indexOf(makeObj1);
    int indexMake2 = Arrays.asList(makes).indexOf(makeObj2);

    if (indexMake1 == -1) indexMake1 = makes.length;
    if (indexMake2 == -1) indexMake2 = makes.length;

    return indexMake1 - indexMake2;
}

可能的解決方案(如果您不想使用Collections.sort)可以使用鑰匙索引計數,其中汽車的品牌為鑰匙(桶)。

參考: http : //www.cs.princeton.edu/courses/archive/spr13/cos226/demo/51DemoKeyIndexedCounting.pdf

快速實施示例:

public class Vehicle {
    private String m_name;
    private String m_brand;

    public Vehicle(String name, String brand)  {
        m_name = name;
        m_brand = brand;
    }

    public String getBrand() {
        return m_brand;
    }

    @Override
    public String toString()  {
        return "Vehicle: " + m_name + " - " + m_brand;
    }
}


public class KeyIndexCounting 
{
    public void sort(ArrayList<Vehicle> input, ArrayList<String> rules)
    {
        int N = input.size();
        int R = rules.size();
        int[] count = new int[R + 1];
        Vehicle[] aux = new Vehicle[N];

        for(int i = 0; i < N; ++i)
        {
            String brand = input.get(i).getBrand();
            ++count[rules.indexOf(brand) + 1];
        }

        for(int r = 0; r < R; ++r)
            count[r + 1] += count[r];

        for(int i = 0; i < N; ++i)
        {
            String brand = input.get(i).getBrand();
            aux[count[rules.indexOf(brand)]++] = input.get(i);
        }

        for(int i = 0; i < N; ++i)
            System.out.println(aux[i]); // Print sorted output array
    }
}

用法:

    KeyIndexCounting key = new KeyIndexCounting();

    ArrayList<Vehicle> v = new ArrayList<>();
    v.add(new Vehicle("A", "Kia"));
    v.add(new Vehicle("B", "Honda"));
    v.add(new Vehicle("C", "Mazda"));
    v.add(new Vehicle("D", "Kia"));
    v.add(new Vehicle("E", "Honda"));
    v.add(new Vehicle("F", "Mercedes"));
    v.add(new Vehicle("G", "Porsche"));
    v.add(new Vehicle("H", "Honda"));
    v.add(new Vehicle("I", "Kia"));

    ArrayList<String> b = new ArrayList<>();
    b.add("Kia");
    b.add("Mercedes");
    b.add("Honda");
    b.add("Porsche");
    b.add("Mazda");

    key.sort(v, b);

輸出:

車輛:A-起亞
車輛:D-起亞
車輛:我-起亞
車輛:F-奔馳
車輛:B-本田
車輛:E-本田
車輛:H-本田
車輛:G-保時捷
座駕:C-馬自達

編輯:一個版本,只需將品牌中未出現在“規則”數組列表中的所有車輛放到最后:

    int N = input.size();
    int R = rules.size();
    int[] count = new int[R + 1];
    Vehicle[] aux = new Vehicle[N];

    int others = aux.length - 1;

    for(int i = 0; i < N; ++i)
    {
        String brand = input.get(i).getBrand();
        int index = rules.indexOf(brand);
        if(index != -1)
            ++count[index + 1];
        else
            aux[others--] = input.get(i);
    }

    for(int r = 0; r < R; ++r)
        count[r + 1] += count[r];

    for(int i = 0; i < N; ++i)
    {
        String brand = input.get(i).getBrand();
        int index = rules.indexOf(brand);
        if(index != -1)
            aux[count[index]++] = input.get(i);
    }

    for(int i =0; i < N; ++i)
        System.out.println(aux[i]);

    System.out.println("Unsorted vehicles: " + others);

用法:

    KeyIndexCounting sort = new KeyIndexCounting();

    ArrayList<Vehicle> v = new ArrayList<>();
    v.add(new Vehicle("A", "Kia"));
    v.add(new Vehicle("B", "Honda"));
    v.add(new Vehicle("C", "Mazda"));
    v.add(new Vehicle("D", "Kia"));
    v.add(new Vehicle("E", "Honda"));
    v.add(new Vehicle("F", "Mercedes"));
    v.add(new Vehicle("G", "Porsche"));
    v.add(new Vehicle("H", "Honda"));
    v.add(new Vehicle("I", "Kia"));

    ArrayList<String> b = new ArrayList<>();
    b.add("Mazda");
    b.add("Kia");
    b.add("Mercedes");

輸出:

座駕:C-馬自達
車輛:A-起亞
車輛:D-起亞
車輛:我-起亞
車輛:F-奔馳
車輛:H-本田
車輛:G-保時捷
車輛:E-本田
車輛:B-本田
未分類的車輛:4

如您所見,由於本田和保時捷均未出現在列表中,因此最后4輛車未排序。

我做了這項工作,這很簡單,但是我先來看一下@ultddave。

這是我的代碼:

Collections.sort(vehicles, new Comparator<Vehicle>() {
    @Override
    public int compare(Vehicle o1, Vehicle o2) {
        if (o1 == null && o2 == null) {
            return 0;
        }
        if (o1 == null) {
            return -1;
        }
        if (o2 == null) {
            return 1;
        }
        //honda first

        for (int i = 0; i < makes.length; i++) {
            for (int j = 0; j < makes.length; j++) {
                String str1 = o1.getModel().getMakeName().toLowerCase();
                String str2 = o2.getModel().getMakeName().toLowerCase();
                if (i < j) {
                    if (str1.equals(makes[i].toLowerCase())) {
                        return -1;
                    }
                    if (str2.equals(makes[i].toLowerCase())) {
                        return 1;
                    }
                } else if (i == j) {
                    if (str1.equals(makes[i].toLowerCase()) && str2.equals(makes[j].toLowerCase())) {
                        return 0;
                    }
                } else {
                    if (str1.equals(makes[i].toLowerCase())) {
                        return 1;
                    }
                    if (str2.equals(makes[i].toLowerCase())) {
                        return -1;
                    }
                }
            }
        }
        return 0;
    }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM