Advertisement
apl-mhd

Object Sort

Nov 29th, 2017
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.52 KB | None | 0 0
  1. package com.company;
  2.  
  3. import javafx.print.Collation;
  4.  
  5. import java.util.ArrayList;
  6. import java.util.Collection;
  7. import java.util.Collections;
  8.  
  9. public class Main {
  10.  
  11.     public static void main(String[] args) {
  12.  
  13.         ArrayList<String> ob = new ArrayList<>();
  14.  
  15.         ob.add("orko");
  16.         ob.add("orin");
  17.         ob.add("Sabi");
  18.         ob.add("kutaa");
  19.  
  20.         System.out.println(ob);
  21.  
  22.         Collections.sort(ob);
  23.  
  24.         System.out.println(ob);
  25.  
  26.         Student ob1 = new Student(5);
  27.         Student ob2 = new Student(4);
  28.         Student ob3 = new Student(3);
  29.         Student ob4 = new Student(2);
  30.         Student ob5 = new Student(1);
  31.  
  32.         ArrayList<Student> arr = new ArrayList<>();
  33.  
  34.         arr.add( new Student(5));
  35.         arr.add( new Student(4));
  36.         arr.add(new Student(3));
  37.         arr.add( new Student(2));
  38.         arr.add( new Student(1));
  39.  
  40.         Collections.sort(arr, new SortByRoll());
  41.  
  42.         for(int i=0; i<arr.size(); i++) {
  43.             System.out.println(arr.get(i).n);
  44.  
  45.         }
  46.  
  47.  
  48.  
  49.     }
  50. }
  51.  
  52.  
  53. package com.company;
  54.  
  55. public class Student implements Comparable<Student>{
  56.  
  57.  
  58.     public int n;
  59.  
  60.     public Student(int n) {
  61.         this.n = n;
  62.     }
  63.  
  64.     @Override
  65.     public int compareTo(Student o) {
  66.  
  67.         return (this.n - o.n);
  68.     }
  69.  
  70.  
  71. }
  72.  
  73.  
  74.  
  75. package com.company;
  76.  
  77. import java.util.Comparator;
  78.  
  79. public class SortByRoll implements Comparator<Student> {
  80.  
  81.  
  82.     public int compare(Student a, Student b)
  83.     {
  84.         return a.n - b.n;
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement