Roy
Roy

Reputation: 21

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

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

Upvotes: 0

Views: 142

Answers (3)

Nitin kunal
Nitin kunal

Reputation: 1

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;
}

Upvotes: 0

John Gallagher
John Gallagher

Reputation: 535

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;
}

Upvotes: 3

GhostCat
GhostCat

Reputation: 140427

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.

Upvotes: 1

Related Questions