# delete elements from list by rule

I have a list of dates, strings and integers:

lis = {{DateObject[{2000,1,1}],"a",1,"w"},{DateObject[{1999,12,31}],"a",1,"x"},{DateObject[{1999,12,31}],"a",2,"z"}


I would like to identify pairs of elements of lis that have identical values in their 2nd and 3rd position (in this case, "a" and 1), and then delete the older element of the pair, to leave:

res = {{DateObject[{2000,1,1}],"a",1,"w"},{DateObject[{1999,12,31}],"a",2,"z"}}


I realize this is likely be a duplicate question, but can't seem to find it; thanks for any thoughts.

You can do this by first Sorting the list by the date (the First element), reversing the order (so that we have newer first), then using DeleteDuplicatesBy to indicate the second and third column.

DeleteDuplicatesBy[Reverse@SortBy[lis, First], #[[{2, 3}]] &]


Would something like this work for you?

Last[SortBy[#, First]] & /@ GatherBy[lis, #[[{2, 3}]] &]


First, we gather by the 2nd and 3rd element and then, we grab the most recent element from each sublist by sorting them by date.

• Beaten to the punch :) Useful to note DeleteDuplicatesBy though! – Carl Lange Sep 2 '18 at 23:35