Solved

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

Posted on 2014-10-19
5
166 Views
Last Modified: 2016-02-18
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
0
Comment
Question by:bounty457
  • 2
  • 2
5 Comments
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40390127
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
 
LVL 43

Accepted Solution

by:
Chris Stanyon earned 500 total points
ID: 40391094
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
 
LVL 1

Author Comment

by:bounty457
ID: 40494527
Thank you very much,

it works fine.

Kind regrads
0
 
LVL 1

Author Comment

by:bounty457
ID: 40494975
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
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 40494976
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Access properties in nested observable collections 8 23
Two different visual studio versions 3 22
Not showing page correctly 3 30
Remove greater than sign 3 43
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now