java - Sort List Student by Grade -


i have exercise oop. define abstract class human first name , last name. define new class student derived human , has new field – grade. define class worker derived human new property weeksalary , workhoursperday , method moneyperhour() returns money earned hour worker. define proper constructors , properties hierarchy. initialize list of 10 students , sort them grade in ascending order. initialize list of 10 workers , sort them money per hour in descending order. merge lists , sort them first name , last name.

i have create classes: human, student, , worker. want sort list student grade. should code in java ?

package exercise2;  import java.util.*;  public class main {      public static void main(string[] args) {          list<student> students = arrays.aslist(                 new student("tam","le trung ngoc ", "2011"),                  new student("thai","le hoang thai ", "2012"),                  new student("phuong", "vu thi thu ","2011"),                  new student("nguyen", "tran ", "2013"),                  new student("hoang", "pham minh ", "2009"),                  new student("tri","truong hoang ", "2011"),                  new student("tuan", "nguyen anh ","2015"),                  new student("tai", "le minh ", "2015"),                  new student("cuong", "nguyen manh ", "2012"),                  new student("an","pham phuoc ", "2011"));         system.out.println("------------students------------");         system.out.println(students);          system.out.println("------------workers------------");          list<worker> workers = arrays.aslist(                 new worker("an","le minh ",150,8),                 new worker("hai","nguyen thanh ",200,4),                 new worker("lan","nguyen thi ",350,12),                 new worker("a","nguyen van ",150,5),                 new worker("lua","tran thi kim ",150,4),                 new worker("tu","nguyen thanh ",125,6),                 new worker("phong","le trung ",375,8),                 new worker("trang","nguyen thi yen ",500,8),                 new worker("thien","le ngoc ",1500,8),                 new worker("vu","le ",150,8)                 );         system.out.println(workers);     }  } 

use comparator when sorting collection:

collections.sort(students, (student o1, student o2) -> {   return o1.getgrade().compareto(o2.getgrade()); // tune needs }); 

if not using java 8 must use (old-fashioned) new comparator instead of lambda expression.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -