aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Sorting the String Saved in ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Sorting the String Saved in ArrayList" Watch "Sorting the String Saved in ArrayList" New topic
Author

Sorting the String Saved in ArrayList

sakthi moorthy
Ranch Hand

Joined: Oct 17, 2007
Posts: 54
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

Joined: Feb 24, 2006
Posts: 6661
    
    5

Looks like home work. Have a look at the Comparable and Comparator classes


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
sakthi moorthy
Ranch Hand

Joined: Oct 17, 2007
Posts: 54
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

Joined: Feb 24, 2006
Posts: 6661
    
    5

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

Joined: Nov 03, 2009
Posts: 14
Hi There's no method in API to sort by second character, but you can write your own for example:



Sun Certified Programmer for Java Platform 6.0 (96%)
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

Welcome to javaranch Karol. It will be easier to write this using a Comparable or Comparator implementation.
sakthi moorthy
Ranch Hand

Joined: Oct 17, 2007
Posts: 54
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

Joined: Feb 24, 2006
Posts: 6661
    
    5

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

Joined: Oct 13, 2009
Posts: 156
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

Sage of The Monstrous Toad of Mount Myoboku
sakthi moorthy
Ranch Hand

Joined: Oct 17, 2007
Posts: 54
Thanks a lot Rohan your Program is one i searched great work
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
sakthi moorthy
Ranch Hand

Joined: Oct 17, 2007
Posts: 54
Yeah Ankit i got the information regarding the comparator , comparable interfaces , Collections.sort(),Arrays.sort() mechanisms by googling Thanks a lot
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sorting the String Saved in ArrayList