How can i sort a List to get the newest .mp3 Files

Hello,
i'am using in C# a List to save the filenames from one directory.
The Last 17 numbers are the timestamp.

This is the content of myList:

241_3_Rock__20141015210003828.mp3
241_3_Rock__20141016210001752.mp3
241_3_Rock__20141017210001782.mp3
241_3_Rock__20141018210001368.mp3
330_1___20141015210007187.mp3
330_1___20141016210003971.mp3
330_1___20141017210002719.mp3
330_1___20141018210001884.mp3
400_1___20141015210007187.mp3
400_1___20141016210003971.mp3
400_1___20141017210002719.mp3
400_1___20141018210001884.mp3
330_1__600207_20141017084127436.mp3
400_1__500107_20141017084127436.mp3


I need the newest Files with beginning:
241_3_Rock
330_
400_

this should be the result:
241_3_Rock__20141018210001368.mp3
330_1___20141018210001884.mp3
400_1___20141018210001884.mp3

this should be ignored:
330_1__******
400_1__******

result ignored:
330_1__600207_20141017084127436.mp3
400_1__500107_20141017084127436.mp3

Do you have any ideas for this?
Very welcome with source Example.
Thank you very much in advance,
Kind regards
Bounty
LVL 1
bounty457Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Your question states, "How can i sort a List to get the newest .mp3 Files". So from what I get from the question you want to get a sub-group of the files with the newest ones on top ordered by date from the filename and not just sort the list?

We need some more information. For example how do you determine which files are ignored in the list?

In your question you state that, "this should be the result:"

241_3_Rock__20141018210001368.mp3
330_1___20141018210001884.mp3
400_1___20141018210001884.mp3

But from what I can tell this is the sort order by date;

330_1___20141018210001884.mp3
400_1___20141018210001884.mp3
241_3_Rock__20141018210001368.mp3

Can you please explain. Thank you
0
Chris StanyonWebDevCommented:
You've got various moving parts to this, so it will depend on how accurately you can define the 'rules', such as what constitutes your file prefix.

Look at creating a class to represent your mp3, and then use a list of these 'mp3' objects to filter your data using LINQ

Your class might look something like this:

public class mp3
{
    public mp3(String mp3Name)
    {
        String[] parts = mp3Name.Split('_');
        Title = mp3Name;
        FileDate = parts[parts.Length - 1];
        Prefix = parts[0] + "_" + parts[1] + "_" + parts[2];
    }

    public String Title { get; set; }
    public String FileDate { get; set; }
    public String Prefix { get; set; }
}

Open in new window


Now you can loop through your existing list (assuming a list of strings called mp3Items), creating a new mp3 object for each one:

List<mp3> mp3List = new List<mp3>();
foreach (String item in mp3Items)
   mp3List.Add(new mp3(item));

Open in new window


And once you've got your new list you can sort it by date, group it by prefix and select the last item in the group (i.e the latest MP3s):

List<mp3> latestMp3s = mp3List.OrderBy(o => o.FileDate)
    .GroupBy(g => g.Prefix)
    .Select(mp3 => mp3.Last())
    .ToList();

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bounty457Author Commented:
Thank you very much,

it works fine.

Kind regrads
0
bounty457Author Commented:
I've requested that this question be closed as follows:

Accepted answer: 0 points for bounty457's comment #a40494527

for the following reason:

Very Good
0
Chris StanyonWebDevCommented:
Hi Bounty457.

You seem to have accepted your own answer as the answer, when I think you should have been accepting another expert's answer. You haven't responded to either Expert's answer but your last comment indicates that it works.

I've objected to your closing of the question so that you can assign points accordingly.

Thanks

Chris
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming Languages-Other

From novice to tech pro — start learning today.