Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
i want to know is there any way to used orderby new id() and order by <column name=""> with together????
Posted
Comments
Maciej Los 10-Apr-15 6:37am    
What? Could you be more soecific and provide more details?
Kriti Sharma 10400841 10-Apr-15 6:43am    
i have a datalist which has 4 types of data named flower,birds,animal,vegitable and i want to order them like
birds on top than
flower than
animal and than
vegitable

and i also want data with order by newid()
Andy Lanng 10-Apr-15 6:52am    
I have updated my solution to fit your criteria

You can order by two columns for e.g. select * from table1 order by colA,colB
 
Share this answer
 
Comments
Kriti Sharma 10400841 10-Apr-15 7:17am    
thanx a lot, it works fine :), but i am facing one another problem with that
if i implementing paging to this, datalist might show some data repeated at page index changed
I think you should be a bit more specific ^_^

C# linq
C#
  private int TypeValue(string type){
    switch(type.ToLower()){
       case "bird":
         return 0;
       case "flower":
         return 1;
       case "animal":
         return 2;
       case "vegitable":
         return 3;
       default:
         return 100;
    }
}
....
  list = list.OrderBy(i=>TypeValue(i.type)).ThenBy(i=>i.newId);

//paging

  list = list.skip(pageIndex * pageSize).take(pageSize);


c# DataTable
C#
  DataTable dt= new DataTable();


foreach (DataRow row in DT.Rows){
    row("CalculatedColumnName") = TypeValue(row.type);
} 
  DataView dv = new DataView(dt);


  dv.Sort = "CalculatedColumnName, newIdASC";
  
  //paging
  var paging = dv.AsEnumerable().skip(pageIndex * pageSize).take(pageSize)


Sql
SQL
   Select *,
    case type
      when 'bird' then 0
      when 'flower' then 1
      when 'animal' then 2
      when 'vegitable' then 3
    end as TypeSort

 from list
   order by 
    case type 
      when 'bird' then 0
      when 'flower' then 1
      when 'animal' then 2
      when 'vegitable' then 3
    end
-- Yes you do have to type it out again (without the name), unless you use common table expressions (cte) but this is simpler
, newId


SQL with CTE
You will need to create this as a stored procedure
SQL
Sql
<pre lang="SQL">
with firstCTE as ( -- just to get the data and sort column
   Select *,
    case type
      when 'bird' then 0
      when 'flower' then 1
      when 'animal' then 2
      when 'vegitable' then 3
    end as TypeSort
 from list
),
secondCTE as ( -- to set up the RowNumber
   select * , 
   ROW_NUMBER() OVER (ORDER BY TypeSort, newId) AS RowNumber
   -- this will give the items a RowNumber in order of the sort
   from firstCTE 
)
select * from secondCTE
where RowNumber >= (@PageStart * @PageSize) AND RowNumber < ((@PageStart+1) * @PageSize)
order by RowNumber

--This is very inexpensive as the cte's are like views and won't select any data until the last 'select'


javascript
JavaScript
    var sortbyVat= function(a, b){
        a= a.VatRate;
        b= b.VatRate;
        if(a== b) return 0;
        return a> b? 1:-1;
    }
    var sortbyPriceAndVat= function(a, b){
        a= a.Price;
        b= b.Price;
        if(a== b) return sortbyVat(a,b);
        return a> b? 1:-1;
    }

...

    array.sort(sortbyPriceAndVat);



Edit: updated c# for specific use
 
Share this answer
 
v5
Comments
Kriti Sharma 10400841 10-Apr-15 7:17am    
thanx a lot, it also works fine :), but still i am facing one another problem with that
if i implementing paging to this, datalist might show some data repeated at page index changed
Andy Lanng 10-Apr-15 7:39am    
Which option are you using? SQL?
in that case I would advise using a cte. I'll update the sql in my solution to show you.
(I added paging to a few other examples too)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900