-1

I tried running below code:

List<Date> listDates = new ArrayList<Date>();

List<WebElement> elementList= driver.findElements(By.xpath(".//[@id='pricedTransactionDiv']/div"));

SimpleDateFormat dateFormatter = new SimpleDateFormat("EEE dd'th' MMMM yyyy");

for(WebElement we:elementList)
{

    listDates.add(dateFormatter.parse(we.getText()));

}

test.log(LogStatus.INFO,"Results before applying sorting: "+listDates);

System.out.println("Results before applying sorting: "+listDates);

ArrayList<Date> sortedList = new ArrayList<>();   
for(Date s:listDates){

    sortedList.add(s);

}

Collections.sort(sortedList);

test.log(LogStatus.INFO,"Results after applying sorting: "+ sortedList);

System.out.println("Results after applying sorting: "+sortedList);

But it didn't work.

I don't really know where I am going wrong. Any idea?

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
B123
  • 45
  • 1
  • 2
  • 11
  • 1
    In what way not working? Please specify precisely the intended behaviour in in what way the observed behaviour differs. Quote any error message and/or stacktrace verbatim. Without such information we stand no chance of helping you. Thank you. – Ole V.V. Mar 23 '18 at 13:19
  • Lemme guess, your dates included one where the suffix wasn’t “th”, for example “March 23rd”?? – Ole V.V. Mar 23 '18 at 13:21
  • 1
    I recommend you avoid the `SimpleDateFormat` class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Mar 23 '18 at 13:21
  • 1
    Please search Stack Overflow before posting. You can assume every basic date-time question has already been asked and answered. – Basil Bourque Mar 25 '18 at 04:38

2 Answers2

1

Sorts the specified list according to the order induced by the specified comparator.

Your need to modify the below line:

Collections.sort(sortedList);

Instead, use below line of code:

Collections.sort(sortedList, new Comparator<Date>(){

    @Override
    public int compare(Date o1, Date o2) {
        return o1.compareTo(o2);
     }
});
Ali Azam
  • 2,047
  • 1
  • 16
  • 25
  • I don’t think it makes any difference. Could you explain the difference you think it makes? Or did you just miss that `Collections` has *two* sort methods, one taking one argument and one taking two arguments? – Ole V.V. Mar 23 '18 at 13:18
  • @AliAzam Date already implements Comparable so the single argument sort should work. – Grasshopper Mar 23 '18 at 13:29
  • @Grasshopper Can you please provide the right answer so that others with question owner can take help from that answer. – Ali Azam Mar 23 '18 at 13:36
  • @AliAzam thnx a lot for your solution. I really appreciate it. But after adding the lines of code provided by you, it didn't worked. I did debugging , code is breaking at for(WebElement we:elementList). – B123 Mar 23 '18 at 14:43
  • I applied the same code for alphabetical orders(A to Z) and it worked fine by only changing Date to String – B123 Mar 23 '18 at 14:47
  • So your issue is not related to sorting. Issue is in some other points, I think. Element is not getting using the selector `By.xpath(".//[@id='pricedTransactionDiv']/div")` – Ali Azam Mar 23 '18 at 14:50
  • @AliAzam I cross checked By.xpath(".//[@id='pricedTransactionDiv']/div"), it's fine. It is listing all the dates in a page. – B123 Mar 23 '18 at 15:04
  • Try to modify `new SimpleDateFormat("EEE dd'th' MMMM yyyy")` parameter. I think it may be the another cause. – Ali Azam Mar 23 '18 at 15:13
  • @B123, thanks for supplying the information here. However, instead please edit your question and add it there so we have all the information in one place and it will be easier for others to find it. – Ole V.V. Mar 23 '18 at 16:46
0

Answer:

List<Date> obtainedList = new ArrayList<Date>();

String date;

List<WebElement> elementList=
 driver.findElements(By.xpath(prop.getProperty(Xpath)));

for(WebElement we:elementList){
    date=we.getText();

    //Get the date out from "Thursday 8th March 2018"
    int Date = Integer.parseInt(date.replaceAll("^\\D*(\\d+).*", "$1"));
    SimpleDateFormat dateFormatter
            = new SimpleDateFormat("EEE dd"+"'"+getDayOfMonthSuffix(Date)+"'"+ " MMMM yyyy");
    Date Date1=dateFormatter.parse(date);
    obtainedList.add(Date1);
}

test.log(LogStatus.INFO,"Results before applying "+"'"+Condition+"'"+" sorting: "+obtainedList);

ArrayList<Date> sortedList = new ArrayList<>();   

for(Date s:obtainedList){
    sortedList.add(s);                  
}

Collections.sort(sortedList, new Comparator<Date>(){
    @Override
    public int compare(Date o1, Date o2) {
        return o1.compareTo(o2);
    }
});

if(Condition.equals("Newest to oldest")){

    Collections.reverse(sortedList);
}

test.log(LogStatus.INFO,"Results after applying sorting: "+sortedList);




//Ordinal Indicator

//Suffix of date('th','st','nd'......)

public static String getDayOfMonthSuffix(int n) {

    checkArgument(n >= 1 && n <= 31, "illegal day of month: " + n);

        if (n >= 11 && n <= 13) {

            return "th";
        }

        switch (n % 10) {

            case 1:  return "st";

            case 2:  return "nd";

            case 3:  return "rd";

            default: return "th";

        }

    }

It worked for me. Thanks all for your input and time. I appreciate it.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
B123
  • 45
  • 1
  • 2
  • 11