help with sorting an array..

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • PillarOfCreation
    New Member
    • Nov 2007
    • 1

    help with sorting an array..

    hello everybody.. im new here and new to java :)
    please help with your expertise

    i need to sort an array of string objects..by lexicographical order.
    by method compareTo() only.

    cant use array.sort() and such.
    if i have several words in the array. and i need to enter a new word that is lexicographical y lower from one of the words, how do i make room for it ,move the rest, and put it in the right spot..
    i tried bubblesort and it didn\t help..

    plz show me an example of sorting from this kind.
    forever grateful





    [CODE]
    public boolean addWord(Word newWord){

    boolean work = true;
    if (_dic[firstEntry] == null){
    _dic[firstEntry] = new Word (newWord.getHeb rewWord(),newWo rd.getEnglishWo rd());

    //firstEntry++;}

    for (;move<firstEnt ry;move++)
    if (_dic[move].getEnglishWord ().compareTo(ne wWord.getEnglis hWord())<0){
    firstEntry++;
    //_dic[move] =_dic[move+1];
    _dic[firstEntry-11] = new Word (newWord.getHeb rewWord(),newWo rd.getEnglishWo rd());}
    work = true;

    }
    return work;
    }
    [/CODE
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by PillarOfCreatio n
    hello everybody.. im new here and new to java :)
    please help with your expertise

    i need to sort an array of string objects..by lexicographical order.
    by method compareTo() only.

    cant use array.sort() and such.
    if i have several words in the array. and i need to enter a new word that is lexicographical y lower from one of the words, how do i make room for it ,move the rest, and put it in the right spot..
    i tried bubblesort and it didn\t help..

    plz show me an example of sorting from this kind.
    forever grateful





    [CODE]
    public boolean addWord(Word newWord){

    boolean work = true;
    if (_dic[firstEntry] == null){
    _dic[firstEntry] = new Word (newWord.getHeb rewWord(),newWo rd.getEnglishWo rd());

    //firstEntry++;}

    for (;move<firstEnt ry;move++)
    if (_dic[move].getEnglishWord ().compareTo(ne wWord.getEnglis hWord())<0){
    firstEntry++;
    //_dic[move] =_dic[move+1];
    _dic[firstEntry-11] = new Word (newWord.getHeb rewWord(),newWo rd.getEnglishWo rd());}
    work = true;

    }
    return work;
    }
    [/CODE
    How about writing down your algorithm first?
    If you can't get it right, read about it here for bubble sort.
    If you you want a better method then this article is your friend.

    Comment

    • piradie
      New Member
      • Nov 2007
      • 5

      #3
      Originally posted by PillarOfCreatio n
      hello everybody.. im new here and new to java :)
      please help with your expertise

      i need to sort an array of string objects..by lexicographical order.
      by method compareTo() only.

      cant use array.sort() and such.
      if i have several words in the array. and i need to enter a new word that is lexicographical y lower from one of the words, how do i make room for it ,move the rest, and put it in the right spot..
      i tried bubblesort and it didn\t help..

      plz show me an example of sorting from this kind.
      forever grateful





      [CODE]
      public boolean addWord(Word newWord){

      boolean work = true;
      if (_dic[firstEntry] == null){
      _dic[firstEntry] = new Word (newWord.getHeb rewWord(),newWo rd.getEnglishWo rd());

      //firstEntry++;}

      for (;move<firstEnt ry;move++)
      if (_dic[move].getEnglishWord ().compareTo(ne wWord.getEnglis hWord())<0){
      firstEntry++;
      //_dic[move] =_dic[move+1];
      _dic[firstEntry-11] = new Word (newWord.getHeb rewWord(),newWo rd.getEnglishWo rd());}
      work = true;

      }
      return work;
      }
      [/CODE
      To be honest, I don't know what you mean by lexicographical order. Do you mean in alphabetic order? (Sorry, my first language is not English)

      Anyway, you could try sorting them with the Search Sort (I believe its called like that).

      And, to move something up or down, you just use yourArray.set() ;

      You will need two loops for this. The inside one is the insertion loop, and the outside one is the one that controls how many times it will run (size-1) times.

      list.set(j+1, list.get(j))

      I will do the example with numbers.

      Imagine you have in your array the numbers:

      2 5 4 6

      It takes 4, compares it to five, if its bigger does nothing. Since its not, it sets 5 to the next position, and then checks if 4 is bigger than 2, since its not, its ordered. And so on.

      May help, may not, I tried :p

      Comment

      Working...