Question: Please help to make the sortListByID method to use the merge sort. This is my assignment question. -------------------------------------- In this assignment, you are going to

Please help to make the sortListByID method to use the merge sort. This is my assignment question.

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

In this assignment, you are going to be given a file that reads a data file for a number of values, and then sorts them by name. This has been done for you and uses the default Collections.sort. However we also want to be able to sort the users by the ID number.

Your assignment is to implement the merge sort on the array of people, but you will need to use the ID as the key to determine what order they go in. You will not be able to use the default sort since it uses the compareTo method, but you need to compare based on something else.

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

This is the Main class file.

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

import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; import java.util.List; import java.util.ArrayList; import java.util.Collections;

public class MergeSortTesting { private List myList= new ArrayList(); public static void main(String[] args) { MergeSortTesting obj = new MergeSortTesting(); obj.run(); } public void run() { File f = new File("DataFileInClass5.txt"); //File f = new File("DataFileInClass5ver2.txt"); Scanner scan = null; try { scan = new Scanner(f); } catch (FileNotFoundException e) { System.out.println("File Not found"); System.exit(0); } while(scan.hasNext()) { int i = scan.nextInt(); String a = scan.next(); String b = scan.next(); Person temp = new Person(i,a,b); myList.add(temp); System.out.println(temp); } System.out.println(myList); long startTime = System.currentTimeMillis(); Collections.sort(myList); long endTime = System.currentTimeMillis(); sortListByID(myList); long endTime2 = System.currentTimeMillis(); System.out.println("Checking the order"); for (int i=0;i<5;i++) System.out.println(myList.get(i)); System.out.println(); System.out.printf("The computer sort took %d (ms) to run.%n" , endTime-startTime); System.out.printf(" My sort took %d (ms) to run.%n" , endTime2-endTime); } private class Person implements Comparable { private String fName; private String lName; private int ID; public int getID() {return ID;} public String getName() {return fName + " " + lName;} Person(int a, String b, String c) { fName = b; lName = c; ID = a; } public String toString() {return "["+lName +", " + fName + " #" + ID+"]";} public int compareTo(Person o) { if (lName.compareTo(o.lName)==0) return fName.compareTo(o.fName); else return lName.compareTo(o.lName); } } private static void sortListByID (List theList) { } }

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

Then, it is the data file. (DataFileInClass5.txt)

91058312 Malika Schulman 69807422 Krysta Lorraine 8927564 Suk Cesar 196816038 Magda Kastner 194671164 Antone Brunell 105752387 Jeannette Luse 87833366 Dorotha Costigan 178889081 Elina Crandall 185162708 Jerrold Schrimsher 33395580 Magan Frier

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!