3

I have a List of items I call pages.

Each page items has the following:

int id { get; set; }
string filename { get; set; }
int status  { get; set; }

The problem I have is that the filename fields are ordered like this:

1.tif
10.tif

and I need them ordered in the list like this:

1.tif
2.tif

I tried the following without luck:

pageList.Sort((a, b) => String.Compare(a.ImageName, b.ImageName));

Thanks!

Anthony Pegram
  • 123,721
  • 27
  • 225
  • 246
Shane Grant
  • 2,484
  • 4
  • 26
  • 32

5 Answers5

2

Going strictly by your example, you need something like this:

pageList.Sort((a, b) => Int32.Parse(a.ImageName.Replace(".tif", "")).CompareTo(Int32.Parse(b.ImageName.Replace(".tif","")))
Matt Hamsmith
  • 3,955
  • 1
  • 27
  • 42
  • This example look good, but ended up with the same results. Here is what I tried: pageList.Sort((a, b) => String.Compare(Path.GetFileNameWithoutExtension(a.ImageName), Path.GetFileNameWithoutExtension(b.ImageName))); I tried that because before the 1.tif in ImageName I have a path, but the path is always the same. – Shane Grant Jun 08 '11 at 19:29
  • @Shane - The example provided by Matt is solid. The main idea is that you need to extract the integer portion of the filename out so that it can be compared as numbers. Using `String.Compare`, as you stated in your comment, misses the most important part of the example. If you change `ImageName.Replace(...)` to something that extracts the number portion of the string you should be set. – Kaleb Pederson Jun 08 '11 at 20:22
  • @Keleb Ok, I had messed up with the Int32.Parse() but after changing my code to this it is now working: pageList.Sort((a, b) => Int32.Parse(Path.GetFileNameWithoutExtension(a.ImageName)).CompareTo(Int32.Parse(Path.GetFileNameWithoutExtension(b.ImageName)))); – Shane Grant Jun 08 '11 at 20:47
1
using System.Linq; // System.Core.dll

IEnumerable<Page> sequence = pageList.OrderBy(x => x.ImageName); // not in-place sort

List<Page> list = sequence.ToList();
abatishchev
  • 98,240
  • 88
  • 296
  • 433
  • I get this error: System.Collections.Generic.List' does not contain a definition for 'OrderBy' – Shane Grant Jun 08 '11 at 19:17
  • Note that this doesn't sort the list. Rather, it returns a new sequence that will be ordered when you loop through it. – JulianR Jun 08 '11 at 19:17
1

If I understand your question right, you want to sort "numeric" filenames in natural numeric order.

This article might give you some pointers: http://www.codeproject.com/KB/recipes/csnsort.aspx

Joe
  • 122,218
  • 32
  • 205
  • 338
  • Also see [Sorting for Humans : Natural Sort Order](http://www.codinghorror.com/blog/2007/12/sorting-for-humans-natural-sort-order.html) – abatishchev Jun 08 '11 at 19:22
1

If you're looking for a sort order that is sensitive to both alphabetic and numerical order such as that found in Windows Explorer, this is referred to as a "Natural Sort Order".

The following question and answer will be of help:

Natural Sort Order in C#

Community
  • 1
  • 1
Tim Lloyd
  • 37,954
  • 10
  • 100
  • 130
0

I believe:

pageList = pageList.GroupBy(p => p.filename.Substring(p.filename.IndexOf('.') + 1)).
    OrderBy(g => g.Key).SelectMany(g => g.OrderBy(p => p.filename)).ToList();

Would give you the list ordered by extension and then by filename.

FlyingStreudel
  • 4,434
  • 4
  • 33
  • 55