sql-order-by

C# Sort and OrderBy comparison

有些话、适合烂在心里 提交于 2019-11-26 12:19:43
I can sort a list using Sort or OrderBy. Which one is faster? Are both working on same 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.Compare(p1.Name,p2.Name,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); } } Why not measure it: class Program { class NameComparer : IComparer<string> {

Using union and order by clause in mysql

拈花ヽ惹草 提交于 2019-11-26 12:14:13
I want to use order by with union in mysql query. I am fetching different types of record based on different criteria from a table based on distance for a search on my site. The first select query returns data related to the exact place search . The 2nd select query returns data related to distance within 5 kms from the place searched. The 3rd select query returns data related to distance within 5-15 kms from the place searched. Then i m using union to merge all results and show on a page with paging. Under appropriate heading as 'Exact search results' , 'Results within 5 kms' etc Now i want

mysql query order by multiple items

走远了吗. 提交于 2019-11-26 12:09:46
问题 is is possible to order by multiple rows? I want my users to be sorted by last_activity, but at the same time, i want the users with pictures to appear before the ones without Something like this: SELECT some_cols FROM `prefix_users` WHERE (some conditions) ORDER BY last_activity, pic_set DESC; 回答1: SELECT some_cols FROM prefix_users WHERE (some conditions) ORDER BY pic_set DESC, last_activity; 回答2: Sort by picture and then by activity: SELECT some_cols FROM `prefix_users` WHERE (some

MySQL - How to ORDER BY RELEVANCE? INNODB Table

徘徊边缘 提交于 2019-11-26 11:54:37
问题 I\'ve got about 20,000 rows in an INNODB table called \'cards\', so FULLTEXT is not an option. Please consider this table: id | name | description ---------------------------------------------------------- 1 John Smith Just some dude 2 Ted Johnson Another dude 3 Johnathan Todd This guy too 4 Susan Smith Her too 5 Sam John Bond And him 6 John Smith Same guy as num 1, another record 7 John Adams Last guy, promise So, say the user searches for \'John\', I want the result set to be in the order

Use own IComparer<T> with Linq OrderBy

故事扮演 提交于 2019-11-26 11:52:57
I have a generic List<MyClass> where MyClass has a property InvoiceNumber which contains values such as: 200906/1 200906/2 .. 200906/10 200906/11 200906/12 My list is bound to a BindingList<T> which supports sorting with linq: protected override void ApplySortCore( PropertyDescriptor property, ListSortDirection direction) { _sortProperty = property; _sortDirection = direction; var items = this.Items; switch (direction) { case ListSortDirection.Ascending: items = items.OrderByDescending(x => property.GetValue(x)).ToList(); break; case ListSortDirection.Descending: items = items

Best way to save a ordered List to the Database while keeping the ordering

China☆狼群 提交于 2019-11-26 11:47:12
问题 I was wondering if anyone has a good solution to a problem I\'ve encountered numerous times during the last years. I have a shopping cart and my customer explicitly requests that it\'s order is significant. So I need to persist the order to the DB. The obvious way would be to simply insert some OrderField where I would assign the number 0 to N and sort it that way. But doing so would make reordering harder and I somehow feel that this solution is kinda fragile and will come back at me some

SQL Query - Using Order By in UNION

萝らか妹 提交于 2019-11-26 11:16:24
How can one programmatically sort a union query when pulling data from two tables? For example, SELECT table1.field1 FROM table1 ORDER BY table1.field1 UNION SELECT table2.field1 FROM table2 ORDER BY table2.field1 Throws an exception Note: this is being attempted on MS Access Jet database engine ajgreyling Sometimes you need to have the ORDER BY in each of the sections that need to be combined with UNION . In this case SELECT * FROM ( SELECT table1.field1 FROM table1 ORDER BY table1.field1 ) DUMMY_ALIAS1 UNION ALL SELECT * FROM ( SELECT table2.field1 FROM table2 ORDER BY table2.field1 ) DUMMY

How can I do an OrderBy with a dynamic string parameter?

瘦欲@ 提交于 2019-11-26 11:14:46
I want to do this: var orderBy = "Nome, Cognome desc"; var timb = time.Timbratures.Include("Anagrafica_Dipendente") .Where(p => p.CodDipendente == 1); if(orderBy != "") timb = timb.OrderBy(orderBy); Is there an OrderBy overload available that accepts a string parameter? Absolutely. You can use the LINQ Dynamic Query Library, found on Scott Guthrie's blog . There's also an updated version available on CodePlex . It lets you create OrderBy clauses, Where clauses, and just about everything else by passing in string parameters. It works great for creating generic code for sorting/filtering grids,

Ordering by specific field value first

喜你入骨 提交于 2019-11-26 11:12:23
I have a table with 3 columns: id | name | priority -------------------- 1 | core | 10 2 | core | 9 3 | other | 8 4 | board | 7 5 | board | 6 6 | core | 4 I want to order the result set using priority but first those rows that have name=core even if have lower priority. The result should look like this id | name | priority -------------------- 6 | core | 4 2 | core | 9 1 | core | 10 5 | board | 6 4 | board | 7 3 | other | 8 Nerdmaster There's also the MySQL FIELD function . If you want complete sorting for all possible values: SELECT id, name, priority FROM mytable ORDER BY FIELD(name, "core",

Table-Valued function - Order by is ignored in output

时间秒杀一切 提交于 2019-11-26 11:10:29
问题 We are moving from SQL Server 2008 to SQL Server 2012 and immediately noticed that all our table-valued functions no longer deliver their temp table contents in the correctly sorted order. CODE: INSERT INTO @Customer SELECT Customer_ID, Name, CASE WHEN Expiry_Date < GETDATE() then 1 WHEN Expired = 1 then 1 ELSE 0 END from Customer **order by Name** In SQL Server 2008 this function returns the customers sorted by Name. In SQL Server 2012 it returns the table unsorted. The \"order by\" is