0

I am writing a Spell Checking system in Java. The output must be sorted in alphabetical order, with one caveat. If two identical words with different capitalization are both misspelled, they must be sorted so that the word that appears first in the file, is first.

For Example, "Peice coobe peice" would be output as "coobe, Peice, peice" while "peice coobe Peice" would be output as "coobe, peice, Peice"

I am currently doing an insertion sort that uses the string compareToIgnoreCase function, and I have also tried the normal compareTo.

Any suggestions would be helpful.

Here is my code:

private ArrayList<Word> insertionSort(ArrayList<Word> list){
        for (int j = 1; j < list.size(); j++) { 
           String key = list.get(j).getWord();
            int i = j - 1;
            while (i >= 0) {
              if (key.compareToIgnoreCase(list.get(i).getWord()) > 0) {
                break;
              }
              list.set(i + 1, list.get(i));
              i--;
            }
            Word w = new Word(key);
            list.set(i + 1, w);
          }
        return list;
    }

Word is an object with a field of word which is a String

redsoxfan
  • 49
  • 3

0 Answers0