Sort Game Solution

I tried many times but I am note able to sort employees lexographically than I unlocked the editor.In the editorial the solution was given in c++.Please Provide me the solution in JAVA.

Have you written the rest of the code? If yes, please share that and I will help you modify it. That will be more helpful as you will be able to understand it faster. If not, then let me know.

/package whatever //do not write package name here /
import java.util.
;
import java.io.
;

class GFG {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int n = sc.nextInt();
int[] sal = new int[n];
String[] name = new String[n];
for(int i=0;i<n;i++){
name[i] = sc.next();
sal[i] = sc.nextInt();
// sc.nextLine();
}
for(int i=0;i<n-1;i++){
for(int j = i+1;j<n-1-i;j++){
if(sal[j]<sal[j+1]){
int temp = sal[j];
sal[j]=sal[j+1];
sal[j+1]= temp;
String t = name[j];
name[j]= name[j+1];
name[j+1] = t;
}
}
}
for(int i =0;i<n;i++){
System.out.println(name[i]+" "+sal[i]);
}
}
}

I have taken input both name and salary and sorted them in descending order salarywise

Try to solve this question using classes. Make a class ‘employee’ which has 2 attributes name and salary. Now you can have an array of objects of type employee and you will need to sort them. In order to sort them, you can write a sort function that does the following : If the salary of one employee is greater than the other, then it simply sorts them in decreasing order. But if the salaries are equal, then it sorts them on the basis of their names. You can use the ‘s1.equals(s2)’ function for string comparison.
An object oriented approach is always better and more optimized. Usually whenever the data is given in a form that contains multiple attributes for every entity, you should consider using classes to represent them.

If anything is not clear, let me know.

I understood your approach.Can i get the final solution it would help me a lot please.

If you have understood the approach then you should code it yourself. If I write the code for you, it will not help you in any way. If you face any problems while coding this approach, you can always ask here and I will help you out.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

i tried what you are telling him to do but everything is sorted here but the name sorting creating problem.
can you help me out with this.
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {

	int target = sc.nextInt();
	//int n = sc.nextInt();
	Employee[] arr = new Employee[4];
	
	
	
	
	arr[0] = new Employee("eve",78); 
	  
    // initialize the second elements of the array 
    arr[1] = new Employee("bob",79); 

    // so on... 
    arr[2] = new Employee("suzy",86); 
    arr[3] = new Employee("alice",86); 
   
	
	//input(arr);
	sort(arr);
	display(arr,target);
}

/**
public static void input(Employee[] arr) {
	for(int i=0;i<arr.length;i++) {
		arr[i].name=sc.next(" ");
		arr[i].salary=sc.nextInt();
		//arr[i] = new Employee[name,salary];
		
	}
}

**/
public static void sort(Employee[] arr) {
	for(int counter=0;counter<arr.length;counter++) {
		for(int i = 0;i<arr.length-1-counter;i++) {
				if(arr[i+1].salary>arr[i].salary) {
					int temp = arr[i].salary;
					arr[i].salary = arr[i+1].salary;
					arr[i+1].salary=temp;	
					
					
				}else if(arr[i+1].salary==arr[i].salary) {
					String temp1=arr[i].name;
                	arr[i].name = arr[i+1].name;
                	arr[i+1].name=temp1;
				}
			}
		}
	
	
}
public static void display(Employee[] arr,int target) {
	for(int i=0;i<arr.length;i++) {
		if(arr[i].salary>=target) {
			System.out.println(arr[i].name+" "+arr[i].salary);
		}
	}
}

}

class Employee {
public String name;
public int salary;

Employee(String name,int salary){
	this.name = name;
	this.salary = salary;
}