Results 1 to 9 of 9

Thread: how to sort this ?

  1. #1

    Thread Starter
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    how to sort this ?

    hi all i have a little problem i need to order data from my database in this manner:

    let's say i have list of orders
    Code:
    Company         ORDER   |     ORDER TIME
    -----------------------------------------
    Company A |    Order    |        10:00
    Company B |    Order    |        10:01
    Company A |    Order    |        10:02
    Company B |    Order    |        10:10
    Company C |   Order     |        10:03
    -----------------------------------------
    Now i want to sort the orders by Order Time and Company Name since Company A will be the first on the list (since it has the earlier order) i now want that all it's other orders of that company will be displayed.

    I hope you get what i mean, the final order should be something like that:
    Code:
    Company A  - 10:00;
    Company A  - 10:02;
    Company B -  10:01;
    Company B -  10:10;
    Company C -  10:03;
    before i get into code this i wanted to ask if there is quick LINQ solution to this ?
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  2. #2

  3. #3
    Fanatic Member amrita's Avatar
    Join Date
    Jan 2007
    Location
    Orissa,India
    Posts
    888

    Re: how to sort this ?

    But if Company A does not have the earliest time then the result will be different. Assume Company A has time 10:04 and 10:06 . Then Order By company will not give the desired result.

    Company A will be the first on the list (since it has the earlier order
    thanks
    amrita

  4. #4
    Fanatic Member amrita's Avatar
    Join Date
    Jan 2007
    Location
    Orissa,India
    Posts
    888

    Re: how to sort this ?

    I think you need to use Order by and Group by .
    thanks
    amrita

  5. #5

    Thread Starter
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: how to sort this ?

    mmm.. i wish it was so simple,
    I might didn't explained well, i don't want the list to be ordered by company name, i want it to be ordered first by orders time and inside that order i want it to "reorder it" so all the orders of the company which has the first will be displayed and so on...
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  6. #6

  7. #7
    Fanatic Member stlaural's Avatar
    Join Date
    Sep 2007
    Location
    Quebec, Canada
    Posts
    683

    Re: how to sort this ?

    I think you can do it in a SQL statement but you'd need to group by all the columns as you have no aggregate function.
    Code:
    Select Company, Order, OrderTime From Table
    Group By Company, Order, OrderTime
    Order By OrderTime
    Not sure if you can do the same with LINQ though.
    Alex
    .NET developer
    "No. Not even in the face of Armageddon. Never compromise." (Walter Kovacs/Rorschach)

    Things to consider before posting.
    Don't forget to rate the posts if they helped and mark thread as resolved when they are.


    .Net Regex Syntax (Scripting) | .Net Regex Language Element | .Net Regex Class | DateTime format | Framework 4.0: what's new
    My fresh new blog : writingthecode, even if I don't post much.

    System: Intel i7 920, Kingston SSDNow V100 64gig, HDD WD Caviar Black 1TB, External WD "My Book" 500GB, XFX Radeon 4890 XT 1GB, 12 GBs Tri-Channel RAM, 1x27" and 1x23" LCDs, Windows 10 x64, ]VS2015, Framework 3.5 and 4.0

  8. #8

    Thread Starter
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: how to sort this ?

    no cicatrix, thats not what i meant, stlaural i hoped i won't have to sort the list twice (first by group by and then the actual list), I'll wait a bit to see if there is any better way of doing this if not i'll just code the thing.
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  9. #9
    Fanatic Member stlaural's Avatar
    Join Date
    Sep 2007
    Location
    Quebec, Canada
    Posts
    683

    Re: how to sort this ?

    If you build your list from your database with an SQL statement like the one I posted, Your list would already be ordered the way you want it.

    If your list is already ordered by "Order Time" I'd simply try to do a GroupBy including all the columns with linq. I think its worth the try to avoid coding it.
    Alex
    .NET developer
    "No. Not even in the face of Armageddon. Never compromise." (Walter Kovacs/Rorschach)

    Things to consider before posting.
    Don't forget to rate the posts if they helped and mark thread as resolved when they are.


    .Net Regex Syntax (Scripting) | .Net Regex Language Element | .Net Regex Class | DateTime format | Framework 4.0: what's new
    My fresh new blog : writingthecode, even if I don't post much.

    System: Intel i7 920, Kingston SSDNow V100 64gig, HDD WD Caviar Black 1TB, External WD "My Book" 500GB, XFX Radeon 4890 XT 1GB, 12 GBs Tri-Channel RAM, 1x27" and 1x23" LCDs, Windows 10 x64, ]VS2015, Framework 3.5 and 4.0

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width