none
Order of data has changed after sorting date values in data. RRS feed

  • Question

  • Hi,

    I have a custom class with some fields (Subject and StartTime) and custom comparer for sorting the time values and stored data in ArrayList collection.

    Sample Data in collection:

    Subject = App1 and StartTime = 25/11/2019

    Subject = App1 and StartTime = 26/11/2019

    Subject = App1 and StartTime = 27/11/2019

    Subject = App1 and StartTime = 28/11/2019

    Subject = App1 and StartTime = 29/11/2019                           

    Subject = App1 and StartTime = 30/11/2019

    Subject = App1 and StartTime = 01/12/2019

    Subject = App2 and StartTime = 25/11/2019

    Subject = App2 and StartTime = 26/11/2019

    Subject = App2 and StartTime = 27/11/2019

    Subject = App2 and StartTime = 28/11/2019

    Subject = App2 and StartTime = 29/11/2019

    Subject = App2 and StartTime = 30/11/2019

    Subject = App2 and StartTime = 01/12/2019

    Subject = App3 and StartTime = 25/11/2019

    Subject = App3 and StartTime = 26/11/2019

    Subject = App3 and StartTime = 27/11/2019

    Subject = App3 and StartTime = 28/11/2019

    Subject = App3 and StartTime = 29/11/2019

    Subject = App3 and StartTime = 30/11/2019

    Subject = App3 and StartTime = 01/12/2019

    Problem:

    FYI : Date values in collection are from 25/11/2019 to 01/12/2019 (including Saturday and Sunday).                                                                                             

    After sorting the date values, times are sorted properly but objects or records order is changed.

    Expected result:

    Subject = App1 and StartTime = 25/11/2019

    Subject = App2 and StartTime = 25/11/2019

    Subject = App3 and StartTime = 25/11/2019

    Subject = App1 and StartTime = 26/11/2019

    Subject = App2 and StartTime = 26/11/2019

    Subject = App3 and StartTime = 26/11/2019

    Observed result:

    Subject = App1 and StartTime = 25/11/2019

    Subject = App3 and StartTime = 25/11/2019

    Subject = App2 and StartTime = 25/11/2019

    Subject = App1 and StartTime = 26/11/2019

    Subject = App3 and StartTime = 26/11/2019

    Subject = App2 and StartTime = 26/11/2019

    //

    //

    Subject = App3 and StartTime = 28/11/2019

    Subject = App2 and StartTime = 28/11/2019

    Subject = App1 and StartTime = 28/11/2019

    Note: This problem occurs only when collection has Saturday and Sunday values (30/11/2019 and 01/12/2019).

    If don’t have that 2 dates in collection, the records are in correct order as expected result after sorting.

    Here I have attached sample demo & screenshots for your reference.

    Please guide me to have sorted objects in correct order as expected result (with Sat and Sun dates) and let me know if I did anything wrong.

    Thanks,


    • Edited by Piruthiv Wednesday, December 18, 2019 7:09 AM
    Wednesday, December 18, 2019 7:07 AM

Answers

All replies