Results 1 to 6 of 6

Thread: Sort list alphabetically in java

  1. #1
    Join Date
    Dec 2009
    Posts
    178

    Sort list alphabetically in java

    Hello,
    I wanted to sort alphabetically a list of files using their names well obviously. I use the Comparator class with a specific code for that final list is sorted in such a way that files are reorganized alphabetically but is first in the list and then files. Records are well sorted but when it comes to the file, there is a group of 4 / 5 files in the list is sorted again but last in the list. I tried a simple compararTo only in the list of files and I get the same reordering So apparently it is my method that distinguishes file or folder that is a problem when I try. However, if I do a Collections.sort (List), the files are reorganized alphabetically. Any help on this will be highly appreciated. Thanks in advance.

  2. #2
    Join Date
    Dec 2009
    Posts
    263

    Re: Sort list alphabetically in java

    Hello,
    I am tried a similar program like this. Here is the source code I have tried till now. If you find any problems in the code then please guide me.
    Code:
    Public class NameComparator implements Comparator, Serializable{
                Public int compares(Object filen1, filen2 Object){
                    
                    int res =0;
                    File f1 = (File)filen1;
                    File f2 = (File)filen2;
                    
                    String n1 = f1.getName();
                    String n2 = f2.getName();
                    
                    if(n1.equals("Parent Directory")) res = 1;
                    else if(n2.equals("Parent Directory")) res = -1;
                    
                    else if(f1.isDirectory() & & File2.isFile()) res = -1;
                    else if(f2.isDirectory() & & File1.isFile()) res = 1;
                    
                    else if(f1.isDirectory() & & File2.isDirectory()) {
                        if(n1.compareTo(n2) > 0) res = 1;
                        
                    }
                    else if(f1.isFile() & & File2.isFile()) {
                        if(n1.compareTo(n2) > 0) res = 1;   
                    }
                    if(n1.compareTo(n2) > 0) res = 1;
                    else res =-1;
                   / / System.out.println (n1 + "comparedto" + n2 + "" + String.valueOf (res));
                return res;
    
                }

  3. #3
    Join Date
    Nov 2009
    Posts
    335

    Re: Sort list alphabetically in java

    Hello,
    First we code is not very legible, this chain of else if no indentation does not facilitate comprehension: /, is there a reason why you do not break your function with multiple returns to strategic locations ? As for your problem (that I was able to understand your code after having indented ), it seems that the last comparison
    Code:
    			if(name1.compareTo(name2) > 0) 
    				res = 1;
    			else 
    				res =-1;
    			/ / System.out.println (name1 + "comparedto" + name2 + "" + String.valueOf (res));
    			return res;
    No matter what you did before, the function would pass through the loop after the concatenation of if and else crush the preceding results. I strongly advise you to return a result of that you're about to have one instead of pursuing the function in an else.

  4. #4
    Join Date
    Nov 2009
    Posts
    347

    Re: Sort list alphabetically in java

    Hello,
    I must say that I am not totally disagree with you: its formatting else if can make its code more compact and more readable. Why break it function with return since it
    Code:
    if ... 
    else if ... 
    else if ... 
    else if ...
    ...
    There will never be a single case to be verified (through else). That is absolutely correct, I think ceres02 has forgotten else before
    Code:
    if(n1.compareTo(name2) > 0)
    I think you can carry with your code.

  5. #5
    Join Date
    Nov 2009
    Posts
    359

    Re: Sort list alphabetically in java

    Hello,
    I think this is matter of habit, I personally think that instead of an else / if / assignment, an if / return is more appropriate. Matter of habit, as I say, and it would avoid the crushing of the last comparison. I think you can improve your code by include the above stated. If you have any more queries regarding this topic then please do post back.

  6. #6
    Join Date
    Nov 2009
    Posts
    356

    Re: Sort list alphabetically in java

    Hello,
    I was always taught to minimize the maximum points recourse. A return by function / method is the rule. Otherwise it mean that your code is poorly organized and you have to change everything to change the output. (Type you return an int, and it becomes a float, we must recast.) You should try this and if you have any more problem then do post back.

Similar Threads

  1. Replies: 9
    Last Post: 09-01-2012, 11:21 AM
  2. How to Sort Excel Worksheets Alphabetically
    By Capers in forum Windows Software
    Replies: 2
    Last Post: 19-10-2011, 12:14 PM
  3. Replies: 6
    Last Post: 05-10-2010, 12:56 AM
  4. Better way to sort a linked list in c++
    By Juany in forum Software Development
    Replies: 5
    Last Post: 13-02-2010, 04:30 PM
  5. How to sort and list characters in C++
    By Gaganadipika in forum Software Development
    Replies: 5
    Last Post: 29-01-2010, 07:55 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,715,538,371.69434 seconds with 17 queries