Solved

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

Posted on 2014-10-19
5
164 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
Comment Utility
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 42

Accepted Solution

by:
Chris Stanyon earned 500 total points
Comment Utility
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
Comment Utility
Thank you very much,

it works fine.

Kind regrads
0
 
LVL 1

Author Comment

by:bounty457
Comment Utility
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 42

Expert Comment

by:Chris Stanyon
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

772 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

12 Experts available now in Live!

Get 1:1 Help Now