1 2 3 Previous Next 35 Replies Latest reply: Jul 11, 2007 9:19 PM by 807605 RSS

    String compareTo

    807605
      hello SDN community,
      Im new here, if im in the wrong forum for my doubt i say sry in advance.

      I have a problem with the method compareTo from String.
      Example:
      public class Manager {
           
           private LinkedList<String> list_players = new LinkedList<String>();
      
           public Manager(){
                list_players.add("[TDM]High Mage");
                list_players.add("[TDM]Red Warrior");
                list_players.add("[GoD]Zeyta");
                list_players.add("Shaman hp");
                list_players.add("[GoD]Publisher");
                list_players.add("[TDM]Clarky");
                list_players.add("Zasamel");
           }
           public void orderListByName(){
                
                String aux = null;
      
                for(String i: list_players){
                     for(String j: list_players){
                          if(i.compareTo(j) < 0){
                               aux = i;
                               i = j;
                               j = aux;
                          }
                     }
                }
           }
           public void showList(){
                String aux = "";
                for(String s: list_players){
                     aux += s + "\n";
                }
                System.out.println(aux);
           }
           public static void main(String[] args){
                Manager m = new Manager();
                m.showList();
           }
           
      }
      I have this code here, and wat it shows in the showList() method is this:

      [TDM]High Mage
      [TDM]Red Warrior
      [GoD]Zeyta
      Shaman hp
      [GoD]Publisher
      [TDM]Clarky
      Zasamel

      My question: Why the names arent ordered? Am i doing a mistake? The only thing i think its wrong its prolly the ASCII code for the "[" or the pointers.
      I think theres actually a better way to order this.
        • 1. Re: String compareTo
          807605
          do you call "orderListByName" anywhere?
          • 2. Re: String compareTo
            807605
            ooops my mistake, ill send the right one

            public class Manager {
                 
                 private LinkedList<String> list_players = new LinkedList<String>();
            
                 public Manager(){
                      list_players.add("[TDM]High Mage");
                      list_players.add("[TDM]Red Warrior");
                      list_players.add("[GoD]Zeyta");
                      list_players.add("Shaman hp");
                      list_players.add("[GoD]Publisher");
                      list_players.add("[TDM]Clarky");
                      list_players.add("Zasamel");
                 }
                 public void orderListByName(){
                      
                      String aux = null;
            
                      for(String i: list_players){
                           for(String j: list_players){
                                if(i.compareTo(j) < 0){
                                     aux = i;
                                     i = j;
                                     j = aux;
                                }
                           }
                      }
                 }
                 public void showList(){
                      orderListByName();
                      String aux = "";
                      
                      for(String s: list_players){
                           aux += s + "\n";
                      }
                      System.out.println(aux);
                 }
                 public static void main(String[] args){
                      Manager m = new Manager();
                      m.showList();
                 }
                 
            }
            Result in console:

            [TDM]High Mage
            [TDM]Red Warrior
            [GoD]Zeyta
            Shaman hp
            [GoD]Publisher
            [TDM]Clarky
            Zasamel
            • 3. Re: String compareTo
              807605
              You realise that all you are doing is changing what Strings the variables i and j are referencing. You don't alter the list in any way.
              • 4. Re: String compareTo
                807605
                You realise that all you are doing is changing what
                Strings the variables i and j are referencing. You
                don't alter the list in any way.
                just noticed that :(

                could always to an array sort method...
                    public void secondOrder()
                    {
                        String[] strList = list_players.toArray(new String[list_players.size()]);
                        Arrays.sort(strList);
                        for (int i = 0; i < strList.length; i++)
                        {
                            System.out.println(strList);
                }
                }
                • 5. Re: String compareTo
                  807605
                  Your orderListByName() method doesn't do anything! Well, OK it does something. This is what it does:

                    * Declares three variables: aux, i and j
                    * Assigns lots of different values to these variables
                    * Returns

                  It is important to realise that once once the method returns those variables are inaccessible and their (final) values have no impact on the rest of the program. And even more important to realise that assigning values to these variables has absolutely no effect on the contents of list_players.

                  After all why should assigning a value to a variable you have just declared have an effect on one you declared earlier? Regarding orderListByName(), Shakespeare's description of something "full of sound and fury, signifying nothing" springs to mind.

                  If you want orderListByName() to alter the list_players list, it will have to call some methods of list_players: get(i) to get the String at position i, and set(i,str) to set it to a new value. The loops will now use a list index.

                  (Because this is such a common thing to want to do, the Java Collections class has a static sort() method that lets you sort a list like list_players.)
                  • 6. Re: String compareTo
                    807605
                    With Collections.sort() works but my real program doesn't use the Object "String" but an Object "Player" and in that class i cant make a compareTo because tht class doesnt control the tags (the tags are the [TDM] and the [GoD]).
                    So i thought on making an algorithm for arraging the array..but it seems it doesn't work, though i didnt understand quite well what you refered about the orderListByName() not doing anything.
                    • 7. Re: String compareTo
                      807605
                      Write your own Comparator and use the Collections.sort method that takes a second parameter.
                      • 8. Re: String compareTo
                        807605
                        Ooooh i understood wat u guys meant, ill try wat petes1234 said and see wat happens, thanks for the support
                        • 9. Re: String compareTo
                          807605
                          Why? That's extra work you don't need to do.
                          • 10. Re: String compareTo
                            807605
                            You mean making an Interface named Comparator with the compareTo method in it?
                            • 11. Re: String compareTo
                              807605
                              Sorta, you write a class that implements Comparator.
                              • 12. Re: String compareTo
                                807605
                                ok ill show u guys the real thing, im not sure if im understanding wat u guys are saying or if its u guys tht arent understanding, i have a class called Player here:
                                public class Player {
                                     
                                     private int id = 0;
                                     private String name;
                                     private int clan_id = 0;
                                     private static int next_id = 1;
                                     
                                     protected Player(String name, int clan_id){
                                          this.id = next_id;
                                          this.name = name;
                                          this.clan_id = clan_id;
                                          next_id++;
                                     }
                                     
                                     public Player(Player p){
                                          this.id = p.id;
                                          this.name = p.name;
                                          this.clan_id = p.clan_id;
                                     }
                                     
                                     public boolean equals(Object o){
                                          assert o instanceof Player;
                                          Player p = (Player)o;
                                          
                                          if(name.equals(p.name)){
                                               return true;
                                          }
                                          return false;
                                     }
                                
                                     public String toString(){
                                          return id + " - " + name + " - " + clan_id;
                                     }
                                }
                                And i have a class Clan
                                public class Clan {
                                     
                                     private int id = 0;
                                     private String clan_name;
                                     private String tag;
                                     private static int next_id = 1;
                                     
                                     protected Clan(String clan_name, String tag){
                                          assert clan_name != null && tag != null;
                                          this.id = next_id;
                                          this.clan_name = clan_name;
                                          this.tag = tag;
                                          next_id++;
                                     }
                                     public boolean equals(Object o){
                                          assert getClass().equals(o.getClass());
                                          Clan c = (Clan)o;
                                          
                                          if(clan_name.equals(c.clan_name) && tag.equals(c.tag)){
                                               return true;
                                          }
                                          return false;
                                     }
                                     public String toString(){
                                          return id + " - " + clan_name + " - " + tag;
                                     }
                                To summarize:
                                The Clan class gives the tags to the players with the clan_id linking to clans (in another Manager class), if i do the compareTo in the Player class the tags wont be there, any suggestions?
                                • 13. Re: String compareTo
                                  807605
                                  JavaWorld have an article discussing this: http://www.javaworld.com/javaworld/jw-12-2002/jw-1227-sort.html
                                  • 14. Re: String compareTo
                                    807605
                                    Yeah im pretty familiar with using Comparable, why my post in the 2nd from below? :s
                                    1 2 3 Previous Next