首页 > 代码库 > 对自定义对象进行排序(C++/Java)
对自定义对象进行排序(C++/Java)
自定义了对象,如何按照自定对象中某一或多个元素对其进行排序呢?以下分别从C++和java对其进行实现。
一:C++
/* 实现对自定义的对象类型进行排序(按照其中的元素),首先将对象存放在vector中,然后利用algorithm库函数中的sort对其进行排序,需要重写排序函数以函数名 作为函数指针作为sort的第三个参数 */ #include <iostream> #include <string> #include <algorithm> #include <vector> using namespace std; struct students{ int age; string name; }; void addStudents(vector<students> &vec, int age, string name){ students stu; stu.age = age; stu.name = name; vec.push_back(stu); } // 按照年龄进行排序 升序 bool sortByAge(const students &s1, const students &s2){ return s1.age < s2.age; } // 先按照年龄进行排序 年纪一样 按照姓名排序 bool sortByAll(const students &s1, const students &s2){ if(s1.age < s2.age) return true; else if(s1.age > s2.age) return false; else return s1.name < s2.name; } int main(){ vector<students> vec; addStudents(vec, 20, "zhangming"); addStudents(vec, 20, "xiaoming"); addStudents(vec, 20, "xiaoming2"); addStudents(vec, 23, "xiaohong"); addStudents(vec, 22, "xiaofei"); addStudents(vec, 27, "xiaogang"); cout << "排序前:" << endl; for(vector<students>::iterator iter = vec.begin(); iter != vec.end(); ++iter){ cout << (*iter).age << " " << (*iter).name << endl; } sort(vec.begin(), vec.end(), sortByAll); cout << "排序后:" << endl; for(vector<students>::iterator iter = vec.begin(); iter != vec.end(); ++iter){ cout << (*iter).age << " " << (*iter).name << endl; } }
二:Java
主函数sortClass.java:
package com.test.sort; import java.util.ArrayList; import java.util.Arrays; import com.zack.bean.Students; public class sortClass { /* * ArrayList 中存储了自定义的对象类型, 然后通过toArray()转换为Object类型,再使用java.util.Arrays.sort进行排序 * 注意的是排序要重新实现comparator接口进行,作为sort的第二个参数即myComparator对象 */ static ArrayList<Students> arrayList = new ArrayList<Students>(); public static void addStudents(int age, String username){ Students s = new Students(age, username); arrayList.add(s); } @SuppressWarnings("unchecked") public static void main(String[] args) { addStudents(20, "xiaoming"); addStudents(20, "zhangming"); addStudents(20, "xiaoming2"); addStudents(25, "xiaohong"); addStudents(23, "xiaogang"); addStudents(21, "xiaojie"); System.out.println("排序前:"); for(int i = 0; i < arrayList.size(); i++) { System.out.println(arrayList.get(i).getAge() + " ::" + arrayList.get(i).getUsername()); } System.out.println("降排序后:"); Object[] obj = arrayList.toArray(); Arrays.sort(obj, new myComparator()); for(int i = 0; i < obj.length; i++) { System.out.println(obj[i]); } } }
myComparator.java:
package com.test.sort; import java.util.Comparator; import com.zack.bean.Students; public class myComparator implements Comparator{ @Override public int compare(Object o1, Object o2) { // TODO Auto-generated method stub Students s1 = (Students) o1; Students s2 = (Students) o2; if(s1.getAge() < s2.getAge()) return 1; else if(s1.getAge() > s2.getAge()) return -1; else return s1.getUsername().compareTo(s2.getUsername()); } }
Students.java:
package com.zack.bean; public class Students { private int age; private String username; public Students(int age, String username){ this.age = age; this.username = username; } public int getAge(){ return this.age; } public String getUsername(){ return this.username; } public String toString(){ return this.age + "::" + this.username; } }
对自定义对象进行排序(C++/Java)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。