Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is a task I am struggling to solve. We should use the Comparator in the gen

ID: 3633771 • Letter: T

Question

This is a task I am struggling to solve.

We should use the Comparator in the generic way.

import java.util.Comparator;
public class CircleSorter_Color implements Comparator {<Circle>
public int compare (Circle c1, Circle c2) {
/ / Your implementation
}
}

(Write a version of quicksort that takes an array of objects and a Comparator object as input to get the sorted array.
Signature of quicksort will be as under, with calls to the internal quicksort)

public static <E> void quicksort (E [] list,
Comparator <? super E> comparator) {
quicksort (list, 0, list.length - 1, comparator);
}

Explanation / Answer

1):Sort the list of users according to user id. Generally we write the comparator to sort the result as shown in code public class Main { public static void main(String[] args) { List lstUsers = new ArrayList(); lstUsers.add(new User(2, "Amit", "Sharma", "Admin")); lstUsers.add(new User(4, "Tusha", "Koli", "P1")); lstUsers.add(new User(1, "Ashish", "Chudasama", "P2")); lstUsers.add(new User(3, "Amit", "Jhala", "P2")); // sort the user by user id Collections.sort(lstUsers, new Comparator() { @Override public int compare(User u1, User u2) { //for ascending Order return u1.getUserid().compareTo(u2.getUserid()); // for Descending order //return u2.getUserid().compareTo(u1.getUserid()); } }); for (User user : lstUsers) { System.out.println(user); } } } Output: UserID (1) First Name (Tusha) Last Name (Koli) UserType (P1) UserID (2) First Name (Riten) Last Name (Jhala) UserType (P1) UserID (3) First Name (Ashish) Last Name (Chudasama) UserType (P2) UserID (4) First Name (Amit) Last Name (Sharma) UserType (Admin) 2):Generic way to sort the any user define class object can be accomplished using combination of generic type and reflection as shown in code >This code describe how to write the generic sorter that sort any types of data types in ascending and descending order. By default sorter sort the result into ascending order. We can change the order by supplying order by operation through overloaded constructors. package com.sorter.multilevel; import java.lang.reflect.Field; import java.util.Comparator; import java.util.Date; class GenericSorter implements Comparator { public static int ASC = 1; public static int DESC = 2; Comparator chainComparator = null; String fieldName; int orderBy = ASC; /** * @param fieldName */ public GenericSorter(String fieldName) { this.fieldName = fieldName; } public GenericSorter(String fieldName, int orderBy) { this.fieldName = fieldName; this.orderBy = orderBy; } /** * @param chainComparator * @param fieldName */ public GenericSorter(String fieldName, Comparator chainComparator) { this.chainComparator = chainComparator; this.fieldName = fieldName; } public GenericSorter(String fieldName, int orderBy, Comparator chainComparator) { this.chainComparator = chainComparator; this.fieldName = fieldName; this.orderBy = orderBy; } @Override public int compare(T o1, T o2) { int result = this.orderBy == ASC ? compareValue(o1, o2) : compareValue( o2, o1); if (result == 0 && chainComparator != null) { result = chainComparator.compare(o1, o2); } return result; } private int compareValue(T o1, T o2) { int returnValue = 0; try { //obtain the filed. Field field = o1.getClass().getDeclaredField(this.fieldName); boolean isAccessible=true; //check field is accessible or not // if not then enable the access if(!field.isAccessible()) { field.setAccessible(true); isAccessible=false; } //Retrieve fields values Object objectO1 = field.get(o1); Object objectO2 = field.get(o2); // check for the number if (objectO1 instanceof Number) { if ((objectO1 != null && objectO2 != null) && (objectO1 instanceof Integer || objectO1 instanceof Long || objectO1 instanceof Byte)) { returnValue = Long.valueOf(objectO1 + "").compareTo( Long.valueOf(objectO2 + "")); } else if ((objectO1 != null && objectO2 != null) && (objectO1 instanceof Double || objectO1 instanceof Float)) { returnValue = Double.valueOf(objectO1 + "").compareTo( Double.valueOf(objectO2 + "")); } } else if (objectO1 instanceof String || objectO1 instanceof Character) { if ((objectO1 != null) && objectO2 != null) { returnValue = String.valueOf(objectO1).compareToIgnoreCase( String.valueOf(objectO2)); } } else if (objectO1 instanceof Date) { returnValue = ((Date) objectO1).compareTo((Date) objectO2); } //reset the field access field.setAccessible(isAccessible); } catch (Exception e) { e.printStackTrace(); } return returnValue; } } 3): The code demonstrates the used of GenericSorter class to sort the list of users in ascending order according to userId field. public class Main { public static void main(String[] args) { List lstUsers = new ArrayList(); lstUsers.add(new User(4, "Amit", "Sharma", "Admin")); lstUsers.add(new User(1, "Tusha", "Koli", "P1")); lstUsers.add(new User(3, "Ashish", "Chudasama", "P2")); lstUsers.add(new User(2, "Riten", "Jhala", "P1")); // sort the user by user id Collections.sort(lstUsers, new GenericSorter("userid")); /** * Note: userid is a field name/variable name of class user */ for (User user : lstUsers) { System.out.println(user); } } } Output: UserID (1) First Name (Tusha) Last Name (Koli) UserType (P1) UserID (2) First Name (Riten) Last Name (Jhala) UserType (P1) UserID (3) First Name (Ashish) Last Name (Chudasama) UserType (P2) UserID (4) First Name (Amit) Last Name (Sharma) UserType (Admin) 4):The code demonstrates the used of GenericSorter class to sort the list of users in descending order according to userId field. public class Main { public static void main(String[] args) { List lstUsers = new ArrayList(); lstUsers.add(new User(4, "Amit", "Sharma", "Admin")); lstUsers.add(new User(1, "Tusha", "Koli", "P1")); lstUsers.add(new User(3, "Ashish", "Chudasama", "P2")); lstUsers.add(new User(2, "Riten", "Jhala", "P1")); // sort the user by user id Collections.sort(lstUsers, new GenericSorter("userid",GenericSorter.DESC)); /** * Note: userid is a field name/variable name of class user */ for (User user : lstUsers) { System.out.println(user); } } } Output: UserID (4) First Name (Amit) Last Name (Sharma) UserType (Admin) UserID (3) First Name (Ashish) Last Name (Chudasama) UserType (P2) UserID (2) First Name (Riten) Last Name (Jhala) UserType (P1) UserID (1) First Name (Tusha) Last Name (Koli) UserType (P1) 5):SQL support the orderby clause by which we can sort the result into multiple levels. The code shows the lists of users are sorted according to FirstName and then UserType public class Main { public static void main(String[] args) { List lstUsers = new ArrayList(); lstUsers.add(new User(2, "Amit", "Sharma", "Admin")); lstUsers.add(new User(4, "Tusha", "Koli", "P1")); lstUsers.add(new User(1, "Ashish", "Chudasama", "P2")); lstUsers.add(new User(3, "Amit", "Jhala", "P2")); // sort the user by user id Collections.sort(lstUsers, new GenericSorter("firstName" ,new GenericSorter("userType"))); /** * Note: userid is a field name/variable name of class user */ for(User user : lstUsers) { System.out.println(user); } } } Output: UserID (2) First Name (Amit) Last Name (Sharma) UserType (Admin) UserID (3) First Name (Amit) Last Name (Jhala) UserType (P2) UserID (1) First Name (Ashish) Last Name (Chudasama) UserType (P2) UserID (4) First Name (Tusha) Last Name (Koli) UserType (P1) 6):SQL support the orderby clause by which we can sort the result into multiple levels. The code 5 shows the lists of users are sorted according to FirstName in ascending order and then UserType in descending order. public class Main { public static void main(String[] args) { List lstUsers = new ArrayList(); lstUsers.add(new User(2, "Amit", "Sharma", "Admin")); lstUsers.add(new User(4, "Tusha", "Koli", "P1")); lstUsers.add(new User(1, "Ashish", "Chudasama", "P2")); lstUsers.add(new User(3, "Amit", "Jhala", "P2")); // sort the user by user id Collections.sort(lstUsers, new GenericSorter("firstName" ,new GenericSorter("userType",GenericSorter.DESC))); /** * Note: userid is a field name/variable name of class user */ for (User user : lstUsers) { System.out.println(user); } } } Output: UserID (3) First Name (Amit) Last Name (Jhala) UserType (P2) UserID (2) First Name (Amit) Last Name (Sharma) UserType (Admin) UserID (1) First Name (Ashish) Last Name (Chudasama) UserType (P2) UserID (4) First Name (Tusha) Last Name (Koli) UserType (P1)
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote