0

I have a list of Airlines deals which has Fare in the form of

C$145.19,C$298.17,CC$398.17,C$876.21,C$1001.71

and the deal is shown on the basis of fare sorted from lower to higher.

I have to create a script which pulls this fare and check whether the deals is appearing on the basis of fare from lower to higher. But I am using the array list as string instead of double.

How to convert the string arraylist into the double so that the arraylist could return sorted value?

Code:

ArrayList<String> dealList = new ArrayList<>();

List<Webelement> deals = driver.findelements(By.xpath//"div[@class='xyz']");
// It pulls out all the Fare with same Xpath which is almost 10 value.

for(Webelement w: deals)
{
deallist.add(w.gettext());
}

ArrayList<String> newDeaList = new ArrayList<>();

for(String s: dealList)
{
newDealList.add(s);
}

Collections.sort(newDealList);
Assert.Assertequals(dealList,newDealList);

But I am not getting the correct output .

Output Value:

C$145.19,C$298.17,CC$398.17,C$876.21,C$1001.71

GhostCat
  • 137,827
  • 25
  • 176
  • 248
Roy
  • 21
  • 1
  • 1
  • 8
  • you don't need to turn it into a number, and as long as that C$ are in the values, you can't either – Stultuske May 04 '17 at 18:17
  • Side note: you want to provide a **real** [mcve] here. The code you are showing doesn't give hints what is going on. – GhostCat May 04 '17 at 18:40
  • See on a webpage we have list of airline deals are showing from certain origin to destination with certain fares. The deals are already sorted on the basis of fare. I have to create a script for it so that the deals are sorted. My approach. I have taken webelement of type list and pulled the xpath of it. Then i have added the list into array list and pulled the value by gettext. Now in another list i have copied the same value and applied collection.sort to sort it and then comparing both list which should return true as the first list already return sorted value – Roy May 04 '17 at 18:55

3 Answers3

3

I would suggest something like:

Collections.sort(stringArray, (s1, s2) -> {
    return Double.valueOf(s1.split('$')[1])
        .compareTo(Double.valueOf(s2.split('$')[1]));
});

Alternatively, you could do something like this:

Collections.sort(stringArray.stream().map(s -> Double.valueOf(s.split('$')[1])));

All this having been said, you seem to be comparing equality of two different arrays at the end of your code-snippet, an operation that will always return false as they will be comparing address equality.

EDIT: (After clarification on the problem - here is how to compare two arrays for equality)

boolean compareArrays(List<String> arr1, List<String> arr2) {
    if (arr1.size() != arr2.size()) {
        return false;
    }
    for(int i = 0; i < arr1.size(); i++) {
        if (!arr1.get(i).equals(arr2.get(i)) {
            return false;
        }
    }
    return true;
}
John Gallagher
  • 525
  • 4
  • 15
  • I should not change the above code. Only instead of Collection & Assert True i should integrate this code – Roy May 04 '17 at 18:27
  • Am I to understand that your problem is checking equality between the two arrays? Or in sorting? – John Gallagher May 04 '17 at 18:30
  • 1
    Nice answer for a newbie, worth an upvote! And just in case you intend to practice the newly gained upvote privilege ... I am glad to help ;-) – GhostCat May 04 '17 at 18:41
  • See on a webpage we have list of airline deals are showing from certain origin to destination with certain fares. The deals are already sorted on the basis of fare. I have to create a script for it so that the deals are sorted. My approach. I have taken webelement of type list and pulled the xpath of it. Then i have added the list into array list and pulled the value by gettext. Now in another list i have copied the same value and applied collection.sort to sort it and then comparing both list which should return true as the first list already return sorted value from webpage – Roy May 04 '17 at 18:54
1

Converting string values into double is fully outlined here for example. In your case, you want to look into using DecimalFormat - you specify a pattern that can be used to extract the "numberic" parts of those strings.

Beyond that, it is clear that a comparison of a sorted list of strings and an unsorted version of that list should result in false - as most likely, both lists have the same elements, but in different order! In any case: you could still go in and simply iterate both lists manually and compare each entry to identify the one causing the mismatch; to then have a closer look.

Finally: and keep in mind that using double/Double to represent currency is possible, but a no-go in "real world" applications. Floating point numbers come with rounding errors; thus you should rather look into using BigDecimal.

Community
  • 1
  • 1
GhostCat
  • 137,827
  • 25
  • 176
  • 248
0
private boolean ValidateSortedList() {
    List<String> stringList = new ArrayList<>();
    List<Double> pageList = new ArrayList<>();

    Collections.addAll(stringList, "C$145.19", "C$298.17", "C$398.17");
    System.out.println("deal list in selenium: " + stringList);

    for (String fareInString : stringList) {
        int indexOfFare = findIndex(fareInString);

        Double fareInDouble = Double.parseDouble(fareInString.substring(indexOfFare));
        pageList.add(fareInDouble);
    }

    List<Double> sortedList = new ArrayList<>(pageList);

    Collections.sort(sortedList);
    System.out.println("web page :" + pageList);
    System.out.println("selenuim test list :" + sortedList);
    boolean equals = sortedList.equals(pageList);

    System.out.println("is list equal :"
            + sortedList.equals(pageList));
    return equals;

}

private int findIndex(String str) {
    for (int i = 0; i < str.length(); i++) {
        char c = str.charAt(i);
        if (Character.isDigit(c)) {
            return i;
        }
    }
    return 0;
}