Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-10-19
5
Medium Priority
?
177 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 64

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 44

Accepted Solution

by:
Chris Stanyon earned 2000 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 44

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The purpose of this article is to demonstrate how we can use conditional statements using Python.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The viewer will learn how to implement Singleton Design Pattern in Java.

721 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