3

Hello I need to sort all the files I have in a folder but I got stuck because I need to sort them by index. I'll give you an example:

My files are formatted like this: dateHoursMinutes_index_nameOfFile.dat

Previous I used Array.Sort(_myFiles); to sort them all but now I need to sort them by index, in order.

How can I use linq to do that?

Thank you

sebba23
  • 544
  • 7
  • 24
  • 3
    Possible duplicate of [How to order list of strings on number substring using C#](https://stackoverflow.com/questions/41625535/how-to-order-list-of-strings-on-number-substring-using-c-sharp) – Lucifer Jun 26 '18 at 10:33

2 Answers2

5

Please refer to the following sample code:

string[] _myFiles = new string[4]
{
    "dateHoursMinutes_4_nameOfFile",
    "dateHoursMinutes_1_nameOfFile",
    "dateHoursMinutes_3_nameOfFile",
    "dateHoursMinutes_2_nameOfFile"
};

char[] sep = new char[1] { '_' };
string[] sorted = _myFiles
    .OrderBy(x => Convert.ToInt32(x.Split(sep)[1]))
    .ToArray();
mm8
  • 163,881
  • 10
  • 57
  • 88
  • 1
    nice answer sir never have thought of it – Lucifer Jun 26 '18 at 09:56
  • If your file format is strict enough that it will always have the character _ as the first instance prior to your order index that you are interested in this will work very nicely – Fuzzybear Jun 26 '18 at 09:59
  • 2
    I think index should be ordered by int rather than string. So that it would be 1,2,3,10 rather than 1,10,2,3 – Mark Jun 26 '18 at 09:59
  • 1
    @Mark: Good catch. I modified the solution slightly by converting the string to an int. – mm8 Jun 26 '18 at 10:01
  • and what if I have this format "\\Programs\\Drop_99\\recepes\\CLF\\20180626113520_2_WAVES.dat" ? – sebba23 Jun 26 '18 at 10:38
  • What index do you want to sort by? 99 or 2? This is not the format you mentioned in your question. Obviously the solution will differ depending on your actual data (filenames). – mm8 Jun 26 '18 at 11:13
  • the index i want to sort by is 2 – sebba23 Jun 26 '18 at 12:00
  • @sebba23: Just replace the index 1 by 2 then; `OrderBy(x => Convert.ToInt32(x.Split(sep)[2]))` – mm8 Jun 26 '18 at 12:05
2

Try using Substring of _index_ from main string to do so

Lst<string> fileNames = new List<string>();

var SortedFiles = FileNames.OrderBy(x => Decimal.Parse(path.Substring(path.IndexOf("_") + 1, path.Substring(path.IndexOf("_") + 1).IndexOf("_"))).ToList();

Edit: to sort file name such as \\Programs\\Drop_99\\recepes\\CLF\\20180626113520_2_WAVES.dat use Path.GetFileName()

Solution using below answer as ref.

 var sampl12836 = xspli.OrderBy(ele =>
 {
    var path = Path.GetFileName(ele);
    return Decimal.Parse(path.Split('_')[1]); 
 }).ToList();

Solution using my answer:-

var sorted = FileNames.OrderBy(ele =>
{
    var path = Path.GetFileName(ele);
    int firstIndex = path.IndexOf("_");
    return Decimal.Parse(path.Substring(path.IndexOf("_") + 1, path.Substring(firstIndex + 1).IndexOf("_"))); 
}).ToList();
Lucifer
  • 1,594
  • 2
  • 18
  • 32
  • and what if I have this format "\\Programs\\Drop_99\\recepes\\CLF\\20180626113520_2_WAVES.dat" ? – sebba23 Jun 26 '18 at 10:38