Custom Sorting in Java

How Custom Sorting will work in java

# Some time we may got the requirement to Sort generic Class level list into Default Sorting order in that case we will have to write own custom logic for that . below is the code to perform Custom logic in java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com;

import java.util.*;

/**
 *
 * @author anand
 */
public class CustomSorting {

    private String username;
    private String userplace;
    private String userdesignation;

    static List<CustomSorting> details = null;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getUserplace() {
        return userplace;
    }

    public void setUserplace(String userplace) {
        this.userplace = userplace;
    }

    public String getUserdesignation() {
        return userdesignation;
    }

    public void setUserdesignation(String userdesignation) {
        this.userdesignation = userdesignation;
    }

    public CustomSorting(String username, String userplace, String userdesignation) {
        this.username = username;
        this.userplace = userplace;
        this.userdesignation = userdesignation;
    }

    public static void main(String[] args) {
        details = new ArrayList<CustomSorting>();

        CustomSorting customDetails = new CustomSorting("Anand", "Bangalore", "Software Developer");
        CustomSorting customDetails1 = new CustomSorting("Ajeet", "Pune", "Testing Lead");
        CustomSorting customDetails3 = new CustomSorting("Antrish", "Chennai", "C# Lead");
        CustomSorting customDetails5 = new CustomSorting("Sachin", "Rewa", "SBI Manager");
        CustomSorting customDetails6 = new CustomSorting("Anand", "Mumbai", "Software Developer");

        details.add(customDetails);
        details.add(customDetails1);
        details.add(customDetails3);
        details.add(customDetails5);
        details.add(customDetails6);

        System.out.println("==========Before Sorting==============");
        for (CustomSorting custom : details) {
            System.out.println(custom.getUsername());
        }

        Collections.sort(details, new Comparator<CustomSorting>() {

            @Override
            public int compare(CustomSorting t, CustomSorting t1) {
                if (t.getUsername().equals(t1.getUsername())) {
                    return t.getUserplace().compareTo(t1.getUserplace());
                } else {

                    return t.getUsername().compareTo(t1.getUsername());
                }

            }

        });

        System.out.println("===========After Sorting =============");
        for (CustomSorting custom : details) {
            System.out.println(custom.getUsername()+"\t"+custom.getUserplace());
        }
    }

}

Comments

Popular posts from this blog

Converting JSONArray into CSV file in java

Online Book Store Website Using Java

How to read CSV File in Java