---
title: "C# Sort and OrderBy comparison"  
description: "C# Sort and OrderBy comparison"  
author: "Anonymous User"  
published: 2014-02-04  
updated: 2014-02-04  
canonical: https://www.mindstick.com/forum/1952/c-sharp-sort-and-orderby-comparison  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# C# Sort and OrderBy comparison

I can sort a list using Sort or OrderBy. Which [one is faster](https://www.mindstick.com/forum/159660/which-one-is-faster-nosql-or-sql)? Are both working on same [algorithm](https://www.mindstick.com/blog/119/implementing-cryptography-in-c-sharp-dot-net-by-using-sha1-algorithm)?

```
List<Person> persons = new List<Person>();persons.Add(new Person("P005","Janson"));persons.Add(new Person("P002","Aravind"));persons.Add(new Person("P007","Kazhal"));
```

1.

persons.Sort((p1,p2)=>[string](https://www.mindstick.com/articles/1527/string-split-in-c-sharp).[Compare](https://www.mindstick.com/forum/2036/propertyinfo-getvalue-unable-to-compare-datetimes)(p1.Name,p2.Name,[true](https://yourviews.mindstick.com/view/81326/boycott-chinese-products-dream-will-come-true)));

2.

```
var query = persons.OrderBy(n => n.Name, new NameComparer());class NameComparer : IComparer<string>{    public int Compare(string x,string y)    {      return  string.Compare(x, y, true);    }}
```

## Replies

### Reply by Pravesh Singh

Hi Ashish,\
Try this:\

```
class Program{    class NameComparer : IComparer<string>    {        public int Compare(string x, string y)        {            return string.Compare(x, y, true);        }    }    class Person    {        public Person(string id, string name)        {            Id = id;            Name = name;        }        public string Id { get; set; }        public string Name { get; set; }    }    static void Main()    {        List<Person> persons = new List<Person>();        persons.Add(new Person("P005", "Janson"));        persons.Add(new Person("P002", "Aravind"));        persons.Add(new Person("P007", "Kazhal"));        Sort(persons);        OrderBy(persons);        const int COUNT = 1000000;        Stopwatch watch = Stopwatch.StartNew();        for (int i = 0; i < COUNT; i++)        {            Sort(persons);        }        watch.Stop();        Console.WriteLine("Sort: {0}ms", watch.ElapsedMilliseconds);        watch = Stopwatch.StartNew();        for (int i = 0; i < COUNT; i++)        {            OrderBy(persons);        }        watch.Stop();        Console.WriteLine("OrderBy: {0}ms", watch.ElapsedMilliseconds);    }    static void Sort(List<Person> list)    {        list.Sort((p1, p2) => string.Compare(p1.Name, p2.Name, true));    }    static void OrderBy(List<Person> list)    {        var result = list.OrderBy(n => n.Name, new NameComparer()).ToArray();    }}
```

On my computer when compiled in Release mode this program prints:

Sort: 1162ms

OrderBy: 1269ms


---

Original Source: https://www.mindstick.com/forum/1952/c-sharp-sort-and-orderby-comparison

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
