2
private class FileType extends Object {
    private String Name;
    private String Type;

    public FileType() {
        Name = null;
        Type = null;
    }

    public void setFileType(String n, String t) {
        Name = n;
        Type = t;
    }   

    public int compareTo(FileType ft) {
        String decodedFileName = null;
        String decodedInputName = null;
        try {
            decodedFileName = URLDecoder.decode(this.Name, "UTF-8");
            decodedInputName = URLDecoder.decode(ft.Name, "UTF-8");
        }
        catch(UnsupportedEncodingException e) {
        }
        return decodedFileName.compareToIgnoreCase(decodedInputName);
    }
}

Above code is my define class for file list.
I had implement compare file name.
The type may Folder or File.
But I want to sort file first priority is Type, and second priority is Name.
How can arrive it?

3
  • 5
    The extends Object is redundant and prevents legitimate inheritence. Commented Aug 10, 2012 at 9:23
  • 1
    I think you should implement comparable interface. And why extending object is implicitly required? Commented Aug 10, 2012 at 9:24
  • 2
    Your compareTo implementation uses neither name nor type. I'm confused when I read your code. And you don't follow the Sun coding standards, which is a bad sign to me. Commented Aug 10, 2012 at 9:26

6 Answers 6

3

You have to implement Comparable / compareTo method.

Sign up to request clarification or add additional context in comments.

Comments

2

Use Comparator Interface from java.util package to sort in more than one way......

Use the compare(T t1, T t2) method of Comparator Eg:

Here is an example from site http://www.mkyong.com

import java.util.Comparator;

public class Fruit{

    private String fruitName;
    private String fruitDesc;
    private int quantity;

    public Fruit(String fruitName, String fruitDesc, int quantity) {
        super();
        this.fruitName = fruitName;
        this.fruitDesc = fruitDesc;
        this.quantity = quantity;
    }

    public String getFruitName() {
        return fruitName;
    }
    public void setFruitName(String fruitName) {
        this.fruitName = fruitName;
    }
    public String getFruitDesc() {
        return fruitDesc;
    }
    public void setFruitDesc(String fruitDesc) {
        this.fruitDesc = fruitDesc;
    }
    public int getQuantity() {
        return quantity;
    }
    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }



    public static Comparator<Fruit> FruitNameComparator 
                          = new Comparator<Fruit>() {

        public int compare(Fruit fruit1, Fruit fruit2) {

          String fruitName1 = fruit1.getFruitName().toUpperCase();
          String fruitName2 = fruit2.getFruitName().toUpperCase();

          //ascending order
          return fruitName1.compareTo(fruitName2);

          //descending order
          //return fruitName2.compareTo(fruitName1);
        }

    };
}

Comments

2

Compare both types. If the comparison is different from 0, return it as a result. If equal to 0, then compare the names.

Note that:

  • Extending Object is unnecessary: it's the default
  • Fields should start with a lower-case letter: name, type na dnot Name, Type
  • Your class should implement Comparable<FileType>
  • I would choose another name for the class: It's not a file type, but a file name associated to a file type
  • I would use an enum rather than a String for file types, since you only have two valid instances of file types
  • You should never ignore exceptions like you're doing. BTW, if this exception occurs, it could lead to a NullPointerException. Wrap the exception into a runtime exception and throw this runtime exception:

    catch(UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
    
  • Your compareTo method doesn't handle null names, although the default constructor assigns null to the name. Fix the method or the constructor. It seems to me that a file name should never be null, so I would fix the constructor.

Comments

1
if (this.Type.equals(ft.Type)){
  return decodedFileName.compareTo(decodedInputName);
}
else{
  return this.Type.compareTo(ft.Type);

}

Comments

1

You first compare the type and then the decoded name. I cached the decodedFileName value directly in the class to prevent calling URLDecoder.decode too much.

private class FileType extends Object implements Comparable<FileType>{
    private String name;
    private String decodedFileName;
    private String type;
    public FileType(String n, String t) {
        name = n;
        try {
            decodedFileName = URLDecoder.decode(this.name, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }           
        type = t;
    }
    public int compareTo(FileType other) {
        int result = type.compareToIgnoreCase(other.type);
        if (result == 0){
            result = decodedFileName.compareToIgnoreCase(other.decodedFileName);
        }
        return result;
    }
}

Comments

1

if you have only two types, why dont make them enum?
then first you compare type.ordinal, if there are equal then compare names, and also prevents from putting unwanted values there

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.