2 Replies Latest reply on May 30, 2008 4:23 AM by 807601

    sort  a vector by name

    807601
      hello all

      i need a help, i make this small code and i get a error when i try to sort a vector by name (String)
      i make a sort by selection , i know that there is an interface ( Comparator ) but it works with Array not vector (i guess i 'm not sur ) this is my email mulder7980@gmail.com , so thanks for help

      public class Adherent {

      private String m_nom;
      private int m_numero;
      private double m_cotisation;

      public Adherent(){ 
      }

      public void saisiAder() {
      BufferedReader ValeurEntrer = new BufferedReader(new InputStreamReaderSystem.in));
      try {
      System.out.println("saisi le nom de l'adherent : ");
      String nom = ValeurEntrer.readLine();
      this.setM_nom(nom);

      System.out.println("saisi le numero de l'adherent : ");
      int num = Integer.parseInt(ValeurEntrer.readLine());
      this.setM_numero(num);

      System.out.println("saisi la cotisation de l'adherent : ");
      double cot = Double.parseDouble(ValeurEntrer.readLine());
      this.setM_cotisation(cot);
                     
      } catch (Exception e) {}
      }

      static Vector v = new Vector();

           public void insertion() {
           Adherent ad = new Adherent(m_nom);
           ad.saisiAder();
           v.addElement(ad);
           }

      public void affichevect(){
      for(int i = 0 ; i<v.size();i++){
      ((Adherent)v.elementAt(i)).afficheAdher();
      }
      }


      public Vector sortvect(){
                
      for(int i=0;i<(v.size())-1;i++){
      for(int j=i+1 ;j<v.size();j++){
           //in this line a got error to '>'
      if (((Adherent)v.elementAt(i)).getM_nom())>( ((Adherent)v.elementAt(j)).getM_nom() ){                 Object temp1 = (Adherent)v.elementAt(i);
      Object temp2 = (Adherent)v.elementAt(j);
      v.setElementAt(temp1, j);
      v.setElementAt(temp2, i);
      }
      }
      }     
      return v ;               
      }
        • 1. Re: sort  a vector by name
          807601
          fox_mulder wrote:
          hello all

          i need a help, i make this small code and i get a error when i try to sort a vector by name (String)
          We don't read minds. You need to copy and paste the entire error message and indicate on which line it occurs.
          i make a sort by selection , i know that there is an interface ( Comparator ) but it works with Array not vector (i guess i 'm not sur )
          You can use Collections.sort(List, Comparator). Since a Vector is a List, it will work.
          • 2. Re: sort  a vector by name
            807601
            [http://java.sun.com/docs/books/tutorial/collections/index.html]