Determines if the list of arrays is sorted.

I need to evaluate if the list of arrays is sorted (not sort).

When the rows are sorted, they are in alphabetical order. I am trying to use compareTo () to determine which row will be first.

And return true if the list of arrays is sorted, otherwise 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; } 

A simple 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 simple test shows only 1/3 coverage.

How to solve this problem.

+4
source share
6 answers

You have a small error in your method. Must 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 cannot be sure that 1 returned ..

+8
source

Change the condition:

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

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

Go through the documentation compareTo ()

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

+2
source

try it

 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; } } 
+1
source

You can write a useful 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 a utility method, you can use it anywhere.

+1
source

you need to change compareTo expression to any positive number that indicates that the previous item is in alphabetical order after the current item, so 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
source

You need to check the unsorted case.

This means that if you assume ascending sorting, the unsorted case will find the element with index i out of order from index i-1 where element[i] < element[i-1] .

0
source

Source: https://habr.com/ru/post/1490015/


All Articles