-2

I am doing some selenium automation test case and i am getting list of web element using the below code.

List<WebElement> list = GlobalVariables.BrowserDriver.findElements(By.xpath(".//*[@id='Tbls_T']/tbody/tr/td[4]"));

and then i am iterating that web element and getting the values using

for (WebElement webElement : list) {

        System.out.println(webElement.getText());

}

so i am getting the values in string format. and sample values are giving below

-100,000 -80,000 0.100 2 87.270 3,000.000

I want to check these values in sorting order or not? for that i think i should convert to integer and then check using some kind of sorting method i guess. for that i have tried to convert the values to a list of integer and then use some sorting algorithm like Guava to check the sorting. because of negative values i am facing difficulty to do that.

Is there any way i can check the sorting order for the above problem and check the order of the values. ?

thanks in advance.

asl
  • 39
  • 5

3 Answers3

0
  1. create 2 copies of your data
  2. convert the first to integers and sort it
  3. convert the second one to integers and leave it
  4. check if the two lists are equal if they are then your list is sorted
Méhdi Màick
  • 157
  • 3
  • 7
  • i cannot convert to integer because of the negative values. getting exception. for example i am getting -100,000 and i tried this to convert this value Integer.parseInt(-100,000); but getting exception because of '-' – asl Feb 09 '17 at 17:05
  • that's because their not integers here is a solution : String s ="-100,000"; s = s.replaceAll(",","."); //to replace , with . s = Double.parseDouble(s); this should fix it – Méhdi Màick Feb 09 '17 at 17:11
0

Please use another list of doubles and Collections.sort(). You may have to remove the commas from strings

public class ConvertToIntSort 
{
    public static void main( String[] args ) 
    {

       String[] strlist = {"-100000","0.100", "2" , "3000.000","87.270", "-80000" };
       java.util.List<String> stringlist =  Arrays.asList(strlist);
       java.util.List<Double> intList = new ArrayList<Double>();
       for(String str : stringlist)
       {          
         //converting and adding to double list  
         intList.add(Double.valueOf(str));

       }
        //Sorting is done here            
        Collections.sort(intList);

       for(Double num : intList)
       {
           System.out.println(num);         

       }

    }
}
Santo
  • 362
  • 1
  • 4
  • 18
0

You want to just check the incoming list is sorted or not. You compare the previous value with current value. If previous value is greater than current value then the list is not sorted.

Sample code:

    WebElement prev=null;
    boolean isSorted = true;
    for (WebElement currentWebElement : list) {

        if (prev == null) {
            prev = currentWebElement;
            continue;
        } else {

            if (Double.compare(Double.parseDouble(prev.getText().replaceAll(",", "")),
                    Double.parseDouble(currentWebElement .getText().replaceAll(",", ""))) > 0) {
                isSorted = false;
                break;
            }

            prev = currentWebElement;

        }

    }
Sumit Kumar
  • 375
  • 1
  • 11