5

I need to estimate if the array list is sorted (don't sort).

When Strings are sorted, they are in alphabetical order. I try to use compareTo() method to determine which string comes first

And return true if the array list is sorted, else false.

Code:

public boolean isSorted()
{
    boolean sorted = true;        
    for (int i = 1; i < list.size(); i++) {
        if (list.get(i-1).compareTo(list.get(i)) != 1) sorted = false;
    }

    return sorted;
}

Easy test:

    ArrayList<String> animals = new ArrayList<String>();
    ArrayListMethods zoo = new ArrayListMethods(animals); 
    animals.add("ape");
    animals.add("dog");
    animals.add("zebra");

    //test isSorted
    System.out.println(zoo.isSorted());
    System.out.println("Expected: true");

    animals.add("cat");
    System.out.println(zoo.isSorted());
    System.out.println("Expected: false");

    animals.remove("cat");
    animals.add(0,"cat");
    System.out.println(zoo.isSorted());
    System.out.println("Expected: false");

    **Output:**
    false
    Expected: true
    false
    Expected: false
    false
    Expected: false

This easy test shows only 1/3 coverage.

How to solve this issue.

catch23
  • 17,519
  • 42
  • 144
  • 217
  • how can you determine if the list is sorted by simply checking just 2 elements? what if those 2 elements were in sort but others are not? – DevZer0 Jul 06 '13 at 06:39

6 Answers6

8

You have a small bug in your method. Should be :

public boolean isSorted()
{
    boolean sorted = true;        
    for (int i = 1; i < list.size(); i++) {
        if (list.get(i-1).compareTo(list.get(i)) > 0) sorted = false;
    }

    return sorted;
}

>0 instead of !=1, you can't be sure that 1 is returned..

Grisha Weintraub
  • 7,803
  • 1
  • 25
  • 45
2

Change the condition :

if (list.get(i - 1).compareTo(list.get(i)) >0)

You should check for >0 instead of !=-1 .

Go through the documentation of compareTo()

the value 0 if the argument string is equal to this string; a value less than 0 if this string is lexicographically less than the string argument; and a value greater than 0 if this string is lexicographically greater than the string argument.

AllTooSir
  • 48,828
  • 16
  • 130
  • 164
1

Try this

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Sort {
public static void main(String []args) {
    List<String> l1=new ArrayList<String>();
    List<String> l2=new ArrayList<String>();
    l1.add("a");
    l1.add("b");
    l1.add("c");

    l2.add("b");
    l2.add("c");
    l2.add("a");

     if(isSorted(l1)){
         System.out.println("already sorted");
     }
    else{
         Collections.sort(l1);
     }
   }
public static boolean isSorted(List<String> list){
    String previous = "";
    for (String current: list) {
        if (current.compareTo(previous) < 0)
            return false;
        previous = current;
    }
    return true;
}
}
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
1

You can write a utily method like isSortedList(List list).

public static boolean isSortedList(List<? extends Comparable> list)
{
    if(list == null || list.isEmpty())
        return false;

    if(list.size() == 1)  
        return true;

    for(int i=1; i<list.size();i++)
    {
        if(list.get(i).compareTo(list.get(i-1)) < 0 )
            return false;
    }

    return true;    
}

As as utility method, you can use it anywhere.

Bertrand88
  • 701
  • 6
  • 14
prasanth
  • 3,502
  • 4
  • 28
  • 42
0

you have to change the compareTo expresion to any positive number, which indicates that the previous element is alphabetically after the current element, hence the list is not ordered

    public boolean isSorted()
    {
        boolean sorted = true;        
        for (int i = 1; i < list.size(); i++) {
            if (list.get(i-1).compareTo(list.get(i)) > 0) sorted = false;
        }

        return sorted;
    }
0

You need to check for unsorted case.

This means that if you are assuming sorting ascending, the unsorted case will be finding an element at index i being out of order from index i-1 where element[i] < element[i-1].

Saher Ahwal
  • 9,015
  • 32
  • 84
  • 152