Home > OS >  How Can i sort the elements inside Local list to specific way
How Can i sort the elements inside Local list to specific way

Time:06-09

I have List contains several Timestamp.toDate() like following

List MyList= [
2022-06-09 10:01:56.874
2022-06-09 11:02:22.874 // it has the same previous days,months but difference times
2022-06-09 12:03:22.874 // it has the same previous days,months but difference times too
2023-06-09 11:00:56.874 // it has the same previous days,months but difference Years
]

Now, how can I remove all elements that has the same days,months with keeping one old element of these days at least and the same with years
so I want the output like following

List MyList= [
    2022-06-09 10:01:56.874 // i will keep this element because it is old one
    2022-06-09 11:02:22.874 // i will delete this because it has same previous day and month
    2022-06-09 12:03:22.874 // i will delete this because it has same previous day and month too 
    2023-06-09 11:00:56.874 // we have here same previous days and month but i will keep it because it has difference year 
    ]

SO the Final

 List MyList= [
    2022-06-09 10:01:56.874
    2023-06-09 11:00:56.874
    ]

I have big List with Big elements like this any one could tell me how to filter my list like this ? thanks

CodePudding user response:

I believe this would give me desired outcome, although I'm not sure if there are more efficient ways to do it. This also assumes the list is sorted.

var result = groupBy(MyList,
        (DateTime datetime) => DateFormat('dd-MM-yyyy').format(datetime))
    .values
    .expand((element) => [element.first])
    .toList();

You will need these imports for the methods used here

 import "package:collection/collection.dart";
 import 'package:intl/intl.dart';

CodePudding user response:

This will do the trick:

 List<DateTime> myList = [
    DateTime(2022, 06, 09, 10, 01),
    DateTime(2022, 06, 09, 11, 02),
    DateTime(2022, 06, 09, 12, 03),
    DateTime(2023, 06, 09, 10, 01),
  ];

  // Important that the list is sorted first.. 
  // You might be able to skip the sorting, as your list already seemed to be sorted  
  myList.sort();

  final res = myList
      .where(
        (e) => e == myList.firstWhere((f) => e.year == f.year && e.month == f.month && e.day == f.day),
      )
      .toList();

  print(myList);
  print(res);

Will print:

[2022-06-09 10:01:00.000, 2022-06-09 11:02:00.000, 2022-06-09 12:03:00.000, 2023-06-09 10:01:00.000]

[2022-06-09 10:01:00.000, 2023-06-09 10:01:00.000]

DartPad:

https://dartpad.dev/bc6cc0ee81d89b56eb358c12a973fafa

CodePudding user response:

I think you are not interested with the time part in the date, you are only interested with the date (year, month, day), so we can build the whole solution around the trick of removing the time part:

First Step: Convert the date to DateTime() objects, for example:

List<DateTime> dateTimes = [
  DateTime.parse('2022-06-09 10:01:56.874'),
  DateTime.parse('2022-06-09 11:02:22.874'),
  DateTime.parse('2022-06-09 12:03:22.874'),
  DateTime.parse('2023-06-09 11:00:56.874'),
];

Second Step: Remove the time part from the dateTimes:

final List<DateTime> dates = dateTimes.map((dateTime) => DateTime(dateTime.year,dateTime.month,dateTime.day)).toList();

Third Step: Define a list to hold the filtered output:

final List<DateTime> filteredDates = [];

Fourth Step: Loop on the dates list and for each date, check if it exists in the filtered list or not, if yes, then ignore it, if not, add it.

Note that it will work because when we removed the time part from the dateTimes all the dates with same year, month, and day become equivalint.

for (DateTime date in dates) {
  if (!filteredDates.contains(date)) {
    filteredDates.add(date);
  }
}

That's it!, The Output:

[2022-06-09 00:00:00.000, 2023-06-09 00:00:00.000]
  • Related