Question

In: Computer Science

Write a comparator that sorts students by Last and First name in ascending order

Write a comparator that sorts students by Last and First name in ascending order

Solutions

Expert Solution

Please see whole code with the comments:

Code:

// Import all necessary library

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class Main
{
public static void main(String[] args)
{
Students[] stu = {
new Students("Smith", "XYZ"),
new Students("Johnson", "DEF"),
new Students("Williams", "Black"),
new Students("Brown", "Yellow"),
new Students("Jack", "Yellow"),
};

// get List view of the students
List<Students> list = Arrays.asList(stu);

// display all students
System.out.println("Complete Students list:");
list.stream().forEach(System.out::println);
  

// Functions for getting first and last names from an Students
Function<Students, String> byFirstName = Students::getFirstName;
Function<Students, String> byLastName = Students::getLastName;

// Comparator for comparing students by first name then last name
Comparator<Students> lastThenFirst =
Comparator.comparing(byLastName).thenComparing(byFirstName);

// sort students by last name, then first name
System.out.printf(
"%nstudents in ascending order by last name then first:%n");
list.stream()
.sorted(lastThenFirst)
.forEach(System.out::println);
}
}
class Students
{
private String firstName;
private String lastName;


// constructor
public Students(String firstName, String lastName)

{
this.firstName = firstName;
this.lastName = lastName;
  
}

// set firstName
public void setFirstName(String firstName)
{
this.firstName = firstName;
}

// get firstName
public String getFirstName()
{
return firstName;
}

// set lastName
public void setLastName(String lastName)
{
this.lastName = lastName;
}

// get lastName
public String getLastName()
{
return lastName;
}


// return Students's first and last name combined
public String getName()
{
return String.format("%s %s", getFirstName(), getLastName());
}

// return a String containing the Students's information
@Override
public String toString()
{
return String.format("%s %s",getFirstName(), getLastName());
}
}

Output:

Complete Students list:
Smith XYZ
Johnson DEF
Williams Black
Brown Yellow
Jack Yellow

students in ascending order by the last name then first:
Williams Black
Johnson DEF
Smith XYZ
Brown Yellow
Jack Yellow

The last two are having a surname like yellow then it will consider the first name while sorting.

Brown and Jack so Brown will come first then Jack since the surname is the same.

While copying this code from your end please ensure the indentation of the code:

Otherwise will give the error

Screenshot of the working code output:

Consider the above two screenshot as a one.

You can change the students list as per your requirements must be followed first and last name format.


Related Solutions

Write a program that sorts prices of 10 tacos in ascending order based on the price,...
Write a program that sorts prices of 10 tacos in ascending order based on the price, using arrays. Requirements: The user enters the name of the taco and then the price of the taco HINT: Two arrays make this problem simpler. One with the names and the other with the prices. The indices indicate the combination. For instance, a taco price at index 5 has its name also at index 5 of the other array. HINT: It is a good...
Purpose: To write an application using the list data structure that sorts objects in ascending order....
Purpose: To write an application using the list data structure that sorts objects in ascending order. Details: Create a class called Rectangle containing the following: Two instance variables, An instance variable of type double used to hold the rectangle’s width. An instance variable of type double used to hold the rectangle’s height. Provide a constructor with two parameters used to initializes each instance variable. The constructor should verify that the specified width and height values are greater than 0.0 and...
Write a program to sort the student’s names (ascending order), calculate students’ average test scores and...
Write a program to sort the student’s names (ascending order), calculate students’ average test scores and letter grades (Use the 10 point grading scale). In addition, count the number of students receiving a particular letter grade. You may assume the following input file data : Johnson 85 83 77 91 76 Aniston 80 90 95 93 48 Cooper 78 81 11 90 48 Gupta 92 83 30 69 87 Muhammed 23 45 96 38 59 Clark 60 85 45 39...
Write the pseudocode that prompts the user for their first and last name. Display the first...
Write the pseudocode that prompts the user for their first and last name. Display the first initial of their first name and their last name to the user. Ask the user to input a phone number. The program checks which part of Colorado a phone number is from using the values below. If the second digit of the phone number is one of the below digits, print the phone number and which part of Colorado it is from. If none...
C++ Write a program that asks a teacher to input a student’s first name, last name,...
C++ Write a program that asks a teacher to input a student’s first name, last name, and four test scores. The program should find the average of the four test scores and should then write the following information to a file named “students.txt” last_name first_name average A student's first name of “XX” should be used as a sentinel value and no numeric grades less than 0 or greater than 100 should be accepted.  The program should then read the information in...
Write a class named Person with data attributes for a person’s first name, last name, and...
Write a class named Person with data attributes for a person’s first name, last name, and telephone number. Next, write a class named Customer that is a subclass of the Person class. The Customer class should have a data attribute for a customer number and a Boolean data attribute indicating whether the customer wishes to be on a calling list. Demonstrate an instance of the Customer class in a simple program. Using python
Write a program that utilizes a Professor class. A professor has a first name, last name,...
Write a program that utilizes a Professor class. A professor has a first name, last name, affiliation, easiness grade, and a helpfulness grade. Grades range from 1 (lowest) to 5 (highest). The Professor class has two constructors. The first constructor initiates the object with just the first name, the last name, and the affiliation. The default value for easiness and helpfulness grade is 3. The second constructor initiates the object using the first name, the last name, the affiliation, and...
Python Code: Write a program to prompt the user to enter a first name, last name,...
Python Code: Write a program to prompt the user to enter a first name, last name, student ID, number of hours completed and GPA for 5 students. Use a loop to accept the data listed below. Save the records in a text file. Write a second program that reads the records from your new text file, then determines if the student qualifies for Dean’s list or probation. Display the records read from the file on screen with appropriate headings above...
write a regular expression that will, using capturing groups, find: last name first name middle name...
write a regular expression that will, using capturing groups, find: last name first name middle name (if available) student ID rank home phone work phone (if available) email program (i.e., S4040) grade Replace the entire row with comma-delimited values in the following order: first name,middle name,last name,program,rank,grade,email,student ID,home phone,work phone Example substitution string for the first student Jane,V,Quinn,S4040,SO,B,[email protected],Q43-15-5883,318-377-4560,318-245-1144,Y
Write a C++ Program to print the first letter of your first and last name using...
Write a C++ Program to print the first letter of your first and last name using stars. Note: 1) Using nested For Loop 2) The number of lines is given by user. 3) Using one Outer loop to print your letters. 4) Print the letters beside each other.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT