Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sorting the String Saved in ArrayList

 
sakthi moorthy
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi to all,
how to sort the Strings saved in ArrayList based on the second character of Those Strings



output has to be
Java
cbz
ocean
ada
jee

All are Sorting according to their Second Characters.
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like home work. Have a look at the Comparable and Comparator classes
 
sakthi moorthy
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way this not the Home Work this is the question i have faced in one interview. I have tried the comparator , comparable interface along with the Collections.sort(),Arrays.sort(). just i want to know is there any implicit methods to do this sorting that's it .
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no way, to my knowledge, that this can be done with a single API call. So I guess your answer was justified
 
Karol Kornecki
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi There's no method in API to sort by second character, but you can write your own for example:

 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to javaranch Karol. It will be easier to write this using a Comparable or Comparator implementation.
 
sakthi moorthy
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
using Comparable , Comparator interface for sorting String data type alone (not a String Variable of Some Class) is not an efficient way i think because
1) Data Saved in the ArrayList is String data type.
2) If we want to use Comparable , Comparator interface we need to create Class for that
3) Resource Usage will go high if the size of the List Increase than the Karol Koranecki Coding

so i prefer Karol Kornecki method great work, keep it up
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is not an efficient way i think because


We need to define efficiency here.

Data Saved in the ArrayList is String data type.


Which is the case regardless of whether you use Comparable or not.

If we want to use Comparable , Comparator interface we need to create Class for that


The memory overhead of an extra class is negligible and can be safely ignored.

Resource Usage will go high if the size of the List Increase than the Karol Koranecki Coding


How did you conclude this without knowing how a Comparable implementation sorts its elements ?

The Comparable and Comparator implementations define clear contracts that code should follow. I would expect a candidate to choose an existing solution / design over writing something anew. This is especially true with this scenario. You cannot use Comparable with Strings. Strings already have a natural ordering defined by Comparable. Comparator is a good answer, unless there was a requirement explicitly asking you not to use a Comparator.
 
rohan yadav
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.util.*;

public class SortSecondChar implements Comparator<String> {
public static void main(String[] args)
{
ArrayList<String> obj = new ArrayList<String>();

obj.add("Java");
obj.add("ocean");
obj.add("cbz");
obj.add("jee");
obj.add("ada");
SortSecondChar sc = new SortSecondChar();
Collections.sort(obj, sc);

System.out.println(obj);
}

public int compare(String one,String two) {


return (one.substring(1)).compareTo((two.substring(1)));

}
}

I think this will give what do you want
 
sakthi moorthy
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Rohan your Program is one i searched great work
 
Ankit Garg
Sheriff
Posts: 9529
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rohan yadav wrote:I think this will give what do you want

Just giving a ready made solution is not solution for the problem. sakthi you need to understand what the program is doing, what is Comparator, what is the use of Collections class etc...
 
sakthi moorthy
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah Ankit i got the information regarding the comparator , comparable interfaces , Collections.sort(),Arrays.sort() mechanisms by googling Thanks a lot
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic