0

My DB table is described via class MyClass:

public class MyClass
{
 public int ID {get; set;}
 public int AnotherID {get; set;}
 public string Variation {get; set;}
}

Prior to BinarySearch I can sort this list via SQL Order By [Variation] and get it sorted by SQL Server or sort using Web Server via List<T>.Sort(). Do these lists will be absolutely equal under any circumstances?

P.S. The column/property of interest is Variation. SQL Server: T-SQL, Web Server: IIS.

  • 1
    No they'll be not - unless you are very careful. You can have, for example, different local-specific sorting in SQL Server (at server, database or even statement level) than you have in code. There you'd have to implement `IComparable`, which could be even different. – Christian.K Apr 12 '17 at 05:11

3 Answers3

1

If you do it at application level, you'll need to implement IComparable<T> to tell .NET how to sort your List. Sort() function of List<T> uses this interface to decide how list members compare to each other. For simple built-in types, this interface is already implemented in the framework. For your own classes, you need to do it yourself.

Instead of using Sort(), you can use LINQ's OrderBy() too, using a lambda function. You can then apply BinarySearch() on the resulting list. Something like this:

var OrigList = context.GetData().ToList(); //or whatever Model function you have
var SortedList = OrigList.OrderBy(i => i.Variation);
var Result = SortList.BinarySearch(/*your condition*/);

Doing it at SQL level is always an option. But you should consider whether you always need the input to be in sorted form. If not, this might create an overhead if this function needs to be called many times.

Edit

Right. You want to know if .NET handles string comparison in exactly the same way as SQL Server handles varchar. This has already been discussed in this SO post.

Community
  • 1
  • 1
dotNET
  • 33,414
  • 24
  • 162
  • 251
  • I am able to sort and do BinarySearch with `IComparer`. That is not a question about IComparer implementation, I am wondering if T-SQL's `order by` procedure can be used as an alternative to `.sort()`. As for now, I checked it works for a couple examples, but I am not sure if it will work under any circumstances -- that is the question. – Kostya Kartavenka Apr 12 '17 at 05:13
  • @KostyaKartavenka: What is the database type of `Variation` column? – dotNET Apr 12 '17 at 05:15
  • nvarchar. Microsoft T-SQL on Azure. – Kostya Kartavenka Apr 12 '17 at 05:23
  • In terms of performance, is it going to be faster for `.Sort()` to sort "pre-sorted" list? – Kostya Kartavenka Apr 12 '17 at 12:57
  • @KostyaKartavenka: Of course. But you should rather stick to sorting it on one end only. – dotNET Apr 12 '17 at 14:56
1

You will have to specify a collation/locale on each end, ensuring they are identical.

Even then, I'm not sure if this is a robust guarantee. Locales can be updated when new versions of Windows come out, and I'm not sure if it's allowed for them to change sort order between versions. The safest solution may be to always sort on the application side.

Cory Nelson
  • 29,236
  • 5
  • 72
  • 110
  • I am using Latin alphabet only, all the diacritic symbols I am converting to their closest equivalent prior to insert. I am trying to remove CPU usage from the Web Server, but probably that is not an option. Thanks for the answer – Kostya Kartavenka Apr 12 '17 at 05:18
0

Yes, it depends upon your column values, for example if you sort using Primary key then the list will be absolutely equal under any circumstances. Considering your ID column Primary key :

var SortedAsc = OrigList.OrderBy(i => i.ID);
var SortedDesc = OrigList.OrderByDescending(i => i.ID);
Prasad Shetty
  • 115
  • 1
  • 5