Multiple Field Sorting using Comparator

Multiple Field Sorting using Comparator:

Comparator is an interface used to perform custom sorting in java. Comparator can also be used to perform multiple field/sequences sorting in java.

Inorder to use comparator to perform custom sorting,

We need to create a class with Comparator interface implementation, then we need to override and provide the custom sorting logics in the compare(Object obj1, Object obj2) method.

Sample Syntax:


package in.javadomain;

import java.util.Comparator;

public class MyComparator implements Comparator {

@Override
public int compare(Object o1, Object o2) {
// TODO Auto-generated method stub
return 0;
}

}

Then we need to call the above comparator like this in Collections.sort(),


Collections.sort(ARRAYLIST_OR_SET_OR_ANY_COLLECTION,new MyComparator());

 

Now we are going to do multiple field sorting for Mobile POJO.

 

MobilePoJo is a POJO class with mobile_price, mobile_model and memory_card_size.

Our Custom Sorting Logic:

 

1. Mobiles should be sorted based on price in ascending order.
2. If two/more than two mobiles prices are same, then it should sort based on memory_card_size in descending order.

 

MobilePOJO.java: [POJO Class]

package in.javadomain;

/**
* @author Naveen kumar Gunasekaran
* Site : Javadomain.in
*
*/
public class MobilePOJO {
String mobileModel;

int mobilePrice;

int memorySizeinGb;

public int getMemorySizeinGb() {
return memorySizeinGb;
}

public void setMemorySizeinGb(int memorySizeinGb) {
this.memorySizeinGb = memorySizeinGb;
}

public String getMobileModel() {
return mobileModel;
}

public void setMobileModel(String mobileModel) {
this.mobileModel = mobileModel;
}

public int getMobilePrice() {
return mobilePrice;
}

public void setMobilePrice(int mobilePrice) {
this.mobilePrice = mobilePrice;
}

}

 

 

MultipleSeqSortingExample.java [Main Class]


package in.javadomain;

import java.util.ArrayList;
import java.util.Collections;

/**
* @author Naveen kumar Gunasekaran
* Site : Javadomain.in
*
*/
public class MultipleSeqSortingExample {

public static void main(String[] args) {

MobilePOJO mp = new MobilePOJO();
ArrayList<MobilePOJO> mobileArrayLst = new ArrayList<MobilePOJO>();
mp.setMobileModel("Nokia 105");
mp.setMobilePrice(1500);
mp.setMemorySizeinGb(16);
mobileArrayLst.add(mp);

mp = new MobilePOJO();
mp.setMobileModel("Nokia 108");
mp.setMobilePrice(1700);
mp.setMemorySizeinGb(32);
mobileArrayLst.add(mp);

mp = new MobilePOJO();
mp.setMobileModel("Nokia 130");
mp.setMobilePrice(1500);
mp.setMemorySizeinGb(32);
mobileArrayLst.add(mp);

mp = new MobilePOJO();
mp.setMobileModel("Nokia 104");
mp.setMobilePrice(1700);
mp.setMemorySizeinGb(8);
mobileArrayLst.add(mp);

mp = new MobilePOJO();
mp.setMobileModel("Nokia 103");
mp.setMobilePrice(2000);
mp.setMemorySizeinGb(64);
mobileArrayLst.add(mp);

mp = new MobilePOJO();
mp.setMobileModel("Nokia 109");
mp.setMobilePrice(1600);
mp.setMemorySizeinGb(64);
mobileArrayLst.add(mp);

System.out.println("Before Sorting \n");
for (MobilePOJO mobilePOJO : mobileArrayLst) {
System.out.println(mobilePOJO.getMobileModel() + " | Rs. " + mobilePOJO.getMobilePrice()
+ " | Memory card Size : " + mobilePOJO.getMemorySizeinGb());
}

System.out.println("\n------------------------------------------------------");
Collections.sort(mobileArrayLst, new MultipleSortingSequenceComparator());

Collections.sort(mobileArrayLst, new MyComparator());

System.out.println(
"\nAfter Sorting [First it will sort based on price(ascending order) then it will sort based on Memory card Size(descending order)] \n");

for (MobilePOJO mobilePOJO : mobileArrayLst) {
System.out.println(mobilePOJO.getMobileModel() + " | Rs. " + mobilePOJO.getMobilePrice()
+ " | Memory card Size : " + mobilePOJO.getMemorySizeinGb());
}

}

}

 

 

MultipleSortingSequenceComparator.java [Comparator for our custom sorting]


package in.javadomain;

import java.util.Comparator;

/**
* @author Naveen kumar Gunasekaran
* Site : Javadomain.in
*
*/
public class MultipleSortingSequenceComparator implements Comparator<MobilePOJO> {

@Override
public int compare(MobilePOJO o1, MobilePOJO o2) {

if (o1.getMobilePrice() > o2.getMobilePrice()) {
return 1;
} else if (o1.getMobilePrice() < o2.getMobilePrice()) {
return -1;
}

if (o1.getMemorySizeinGb() > o2.getMemorySizeinGb()) {
return -1;
} else if (o1.getMemorySizeinGb() < o2.getMemorySizeinGb()) {
return 1;
} else {
return 0;
}
}
}

 

 

Output:

Before Sorting

Nokia 105 | Rs. 1500 | Memory card Size : 16
Nokia 108 | Rs. 1700 | Memory card Size : 32
Nokia 130 | Rs. 1500 | Memory card Size : 32
Nokia 104 | Rs. 1700 | Memory card Size : 8
Nokia 103 | Rs. 2000 | Memory card Size : 64
Nokia 109 | Rs. 1600 | Memory card Size : 64

------------------------------------------------------

After Sorting [First it will sort based on price(ascending order) then it will sort based on Memory card Size(descending order)]

Nokia 130 | Rs. 1500 | Memory card Size : 32
Nokia 105 | Rs. 1500 | Memory card Size : 16
Nokia 109 | Rs. 1600 | Memory card Size : 64
Nokia 108 | Rs. 1700 | Memory card Size : 32
Nokia 104 | Rs. 1700 | Memory card Size : 8
Nokia 103 | Rs. 2000 | Memory card Size : 64

If you see the output, first mobiles are sorted based on the price.

Since two mobiles prices are same [1500 & 1700], it sorts results based on memory card size in descending order].

Hope you are clear with Multiple Field Sorting using comparator.
Feel free to post your comments/suggestions/feedbacks in the below comments section.

 

 

526 total views, 2 views today

About: Mirthbees

Blogger, Innovator, Developer and Enterpreneur. Founder of GuruRecharge, Readymade online recharge script. Lead Developer in Bookrate.in