Results 1 to 3 of 3

Thread: sorting problem

  1. #1
    vasug is offline Member
    Join Date
    Jan 2010
    Posts
    7
    Rep Power
    0

    Smile sorting problem

    hi,
    Quantity column is of type integer. I need to sort it as integers. I am able to sort the quantity column as strings but not integers. I am giving both the string related and integer related code as follows.


    -----------considering quantity as string -----------working-------------
    public String sortByQuantity(){
    Comparator c = new DbRow(){
    public int compare(Object o1, Object o2){
    DbRow n1 = (DbRow)o1; DbRow n2 = (DbRow)o2;
    return n1.getQuantity().compareTo(n2.getQuantity());
    }
    };
    Arrays.sort(dbrowMain , c);
    return null;
    }
    -------considering quantity as integer type----not working--------
    public Integer sortByQuantity(){
    Comparator c = new DbRow(){
    public int compareTo(Object o1, Object o2){
    DbRow n1 = (DbRow)o1; DbRow n2 = (DbRow)o2;

    Integer s1= new Integer(n1.getQuantity());
    Integer s2 = new Integer(n2.getQuantity());
    return s1.compareTo(s2);
    }
    };
    Arrays.sort(dbrowMain , c);
    return null;
    }

    Could you please mention the please give me answer

    Thanks in Advance
    Vasu

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by vasug View Post
    hi,
    Quantity column is of type integer. I need to sort it as integers. I am able to sort the quantity column as strings but not integers. I am giving both the string related and integer related code as follows.


    -----------considering quantity as string -----------working-------------
    public String sortByQuantity(){
    Comparator c = new DbRow(){
    public int compare(Object o1, Object o2){
    DbRow n1 = (DbRow)o1; DbRow n2 = (DbRow)o2;
    return n1.getQuantity().compareTo(n2.getQuantity());
    }
    };
    Arrays.sort(dbrowMain , c);
    return null;
    }
    -------considering quantity as integer type----not working--------
    public Integer sortByQuantity(){
    Comparator c = new DbRow(){
    public int compareTo(Object o1, Object o2){
    DbRow n1 = (DbRow)o1; DbRow n2 = (DbRow)o2;

    Integer s1= new Integer(n1.getQuantity());
    Integer s2 = new Integer(n2.getQuantity());
    return s1.compareTo(s2);
    }
    };
    Arrays.sort(dbrowMain , c);
    return null;
    }

    Could you please mention the please give me answer

    Thanks in Advance
    Vasu
    What is the return type of the method getQuantity()? If it is a String it doesn't surprise me that you can't convert it to Integers that way. Have a look at the Integer.parseInt(... ) method instead; it converts Strings to ints.

    kind regards,

    Jos

  3. #3
    Anjaneyulu is offline Member
    Join Date
    Feb 2010
    Posts
    29
    Rep Power
    0

    Default

    Hello vasu it is better to post the complete code.Then it is easy to trace

Similar Threads

  1. Problem: Arrays and Sorting
    By Rhez in forum New To Java
    Replies: 7
    Last Post: 02-03-2010, 02:18 PM
  2. Sorting JTable (Vectors) Problem
    By ramapple in forum AWT / Swing
    Replies: 6
    Last Post: 07-06-2009, 11:15 PM
  3. sorting problem...
    By mark-mlt in forum New To Java
    Replies: 4
    Last Post: 04-17-2008, 02:15 PM
  4. sorting problem
    By mcal in forum New To Java
    Replies: 1
    Last Post: 02-14-2008, 08:13 AM
  5. Problem with sorting Table
    By sireesha264 in forum Advanced Java
    Replies: 0
    Last Post: 02-08-2008, 02:21 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •