• Post Reply Bookmark Topic Watch Topic
  • New Topic

Insertion sort based on two attributes  RSS feed

 
vivek shankare gowda
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Employee
{
private String firstName;
private String eeid;

}

I want to sort using empoyee firstName.. If two employee names are same, i need to sort based on eeid.. using insertion sort

if (key == "name") {
for (int i = 1; i < list.length; i++)
{
Employee t = list[i];


int j;
for (j = i - 1; j >= 0 && t.getLastName().compareTo(list[j].getLastName())<0; j--)
list[j + 1] = list[j];
list[j + 1] = t;


}


}

It will sort using names.. But if two names are same, it wont sort based on eeid..

I want to achieve only using insertion sort..


EX:

vivek 8
broody 2
chari 3
vivek 5
chari 1


output:

broody 2
chari 1
chari 3
vivek 5
vivek 8
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First off, never compare strings using the "==" operator - use the "equals" method instead.

Secondly, unless this is an academic exercise meant to teach you sorting, the way to sort an array in Java is to use the java.util.Arrays.sort(Object[]) method. As long as your class's compareTo is implemented correctly (see java.lang.Comparable for that), it'll do all the work for you.
 
vivek shankare gowda
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for suggestion.

It s academic exercise. I need to achieve using insertion sort only. I cant use comparator interface and compare method to achieve this.

 
vivek shankare gowda
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,

i implemented compareTo method.. To achieve that..




@Override
public int compareTo(Object arg0)
{
String a = getLastName();
String b = ((Employee)arg0).getLastName();

int c=a.compareTo(b);

if(c==0)
{
int aa = getEeid();
int bb = ((Employee)arg0).getEeid();

c=aa-bb;
}

return c;

}



what if i need to sort using eeId and if two eeids are same..i need to sort based on names...


I want it to work for both the scenarios..

 
Paweł Baczyński
Bartender
Posts: 2074
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your compareTo method probably should be declared as
@Override
public int compareTo(Employee arg0)


Make sure your class implements Comparable<Employee>.
 
vivek shankare gowda
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the above overrided compareto method is working fine.. I can sort based on last name.. but if i want to sort using eeId .. i got to change implementation of compareto method.. how i can overcome this.. in a single program i want to sort based on lastname and also on eeid..

I cannot use comparator and compare method..

 
vivek shankare gowda
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for (int i = 1; i < list.length; i++)
{
Employee t = list[i];
int j;
for (j = i - 1; j >= 0 && t.getLastName().compareTo(list[j].getLastName())<0; j--)
list[j + 1] = list[j];

list[j + 1] = t;

}


in this code .. i tried,like if compareto method returns zero.. then both names are same.. and there to sort, based on eeid as per my requirement.. but i couldnot achieve it..


In insertion sort.. without using sort method and comparator interface.. how i can tackle this..

Please help me out.

thanks

 
fred rosenberger
lowercase baba
Bartender
Posts: 12562
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my suggestion: StopCoding (<--click that).

You need to THINK through this, not CODE through it. Figure out all the scenarios. What do you do if the first names are the same? What do you do if the last names are the same? what do you do if the eeID is the same? What do you do if any or all of those are equal? Which takes priority?

Coding is less about writing code, and more about understanding the problem. Until you can tell us in English what should happen in all the above situation, you should not write a single line of code.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!