基于关键字的Java排序
我有一份车辆清单。我想根据品牌对这些车辆进行分类。顺序在另一个数组中被取消 这段代码对两个品牌的数组进行了很好的排序,“本田”和“起亚”。这与本田在起亚之上的其他车型不谋而合。是否可以对大小会发生变化的数组进行泛型。如果阵列是“雪佛兰”、“道奇”、“福特”呢 谢谢基于关键字的Java排序,java,sorting,keyword,comparator,Java,Sorting,Keyword,Comparator,我有一份车辆清单。我想根据品牌对这些车辆进行分类。顺序在另一个数组中被取消 这段代码对两个品牌的数组进行了很好的排序,“本田”和“起亚”。这与本田在起亚之上的其他车型不谋而合。是否可以对大小会发生变化的数组进行泛型。如果阵列是“雪佛兰”、“道奇”、“福特”呢 谢谢 //This sorts Honda first, Kia second, and others after. final String[] makes = new String[]{"Honda","Kia"}; Collectio
//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
}
});
//这将本田排在第一位,起亚排在第二位,其他排在第二位。
最终字符串[]makes=新字符串[]{“Honda”,“Kia”};
集合。排序(车辆、新比较器(){
@凌驾
公共int比较(车辆o1、车辆o2){
字符串makeObj1=o1.getModel().getMakeName().toLowerCase();
字符串makeObj2=o2.getModel().getMakeName().toLowerCase();
//本田优先
if(makeObj1.equals(makes[0].toLowerCase()){
if(makeObj2.equals(makes[0].toLowerCase()){
返回0;//本田=本田
}
if(makeObj2.equals(makes[1].toLowerCase()){
return-1;//本田>起亚
}否则{
return-1;//本田>其他
}
}
//起亚优先
if(makeObj1.equals(makes[1].toLowerCase()){
if(makeObj2.equals(makes[0].toLowerCase()){
返回1;//起亚<本田
}
if(makeObj2.equals(makes[1].toLowerCase()){
返回0;//kia=kia
}否则{
return-1;//kia>其他
}
}
//本田第二
if(makeObj2.equals(makes[0].toLowerCase()){
if(makeObj1.equals(makes[1].toLowerCase()){
返回1;//起亚<本田
}否则{
返回1;//其他<1
}
}
//起亚秒
if(makeObj2.equals(makes[1].toLowerCase()){
返回1;//其他
没有测试它,但您可以尝试:
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;
}
没有测试,但您可以尝试:
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)可以是使用关键索引计数,其中汽车品牌是关键(桶)
参考:
快速实施示例:
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
}
}
公共级车辆{
私有字符串m_name;
私人字符串m_品牌;
公共车辆(字串名称、字串品牌){
m_name=名称;
m_品牌=品牌;
}
公共字符串getBrand(){
回归m_品牌;
}
@凌驾
公共字符串toString(){
返回“车辆:+m_名称+”-“+m_品牌;
}
}
公共类KeyIndexCounting
{
公共无效排序(ArrayList输入,ArrayList规则)
{
int N=input.size();
int R=rules.size();
int[]计数=新的int[R+1];
车辆[]辅助=新车[N];
对于(int i=0;i
用法:
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);
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");
KeyIndexCounting key=new KeyIndexCounting();
ArrayList v=新的ArrayList();
v、 添加(新车(“A”、“起亚”);
v、 添加(新车(“B”、“本田”);
v、 增加(新车(“C”、“马自达”);
v、 添加(新车(“D”、“起亚”);
v、 添加(新车(“E”、“本田”);
v、 添加(新车(“F”、“梅赛德斯”);
v、 添加(新车(“G”、“保时捷”);
v、 添加(新车(“H”、“本田”);
v、 添加(新车(“I”、“起亚”);
ArrayList b=新的ArrayList();
b、 添加(“起亚”);
b、 添加(“梅赛德斯”);
b、 添加(“本田”);
b、 添加(“保时捷”);
b、 添加(“马自达”);
键。排序(v,b);
输出:
车辆:A-起亚车辆:D-起亚
车辆:I-起亚
车辆: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);
int N=input.size();
int R=rules.size();
int[]计数=新的int[R+1];
车辆[]辅助=新车[N];
int其他=辅助长度-1;
对于(int i=0;i
用法:
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);
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");
KeyIndexCounting排序=新的KeyIndexCounting();
ArrayList v=新的ArrayList();
v、 添加(新车(“A”、“起亚”);
v、 添加(新车(“B”、“本田”);
v、 增加(新车(“C”、“马自达”);
v、 添加(新车(“D”、“起亚”);
v、 添加(新车(“E”、“本田”);
v、 添加(新车(“F”、“梅赛德斯”);
v、 添加(新车(“G”、“保时捷”);
v、 添加(新车(“H”、“本田”);
v、 添加(新车(“I”、“起亚”);
ArrayList b=新的ArrayList();
b、 添加(“马自达”);
b、 添加(“起亚”);
b、 添加(“梅赛德斯”);
输出:
车辆:C-马自达车辆:A-起亚
车辆:D-起亚
车辆:I-起亚
车辆:F-梅赛德斯
车辆:H-本田
车辆:G-保时捷
车辆:E-本田
车辆:B-本田
未分类车辆:4辆
如您所见,最后4辆车是uns