7 Replies - 938 Views - Last Post: 13 May 2011 - 06:25 AM Rate Topic: -----

#1 01hmyoung   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 24
  • Joined: 20-January 10

sorting objects in list

Posted 12 May 2011 - 07:04 AM

i need to be able to sort my list of object into order, i have a list of vicals and i need to sort them by wearer they are sold or not sold. i do a foreachloop to go through the list but the i can get it to sort the list. any help

i will post code if needed
Is This A Good Question/Topic? 0
  • +

Replies To: sorting objects in list

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14243
  • View blog
  • Posts: 57,137
  • Joined: 12-June 08

Re: sorting objects in list

Posted 12 May 2011 - 07:08 AM

Yes post your code.

A few questions:

What is "vicals"?
Can you reexplain your sort condition: "sort them by wearer they are sold or not sold"?
Can you provide some examples of your data you are sorting?
Was This Post Helpful? 0
  • +
  • -

#3 01hmyoung   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 24
  • Joined: 20-January 10

Re: sorting objects in list

Posted 12 May 2011 - 07:13 AM

 private void sortButton_Click(object sender, EventArgs e)
        {
            if (radioButton1.Checked)
            {
                foreach (veical v in _vical)
                {
                    if (v is Car)
                    {
                        
                    }
                }
            }
            if (radioButton2.Checked)
            {
                foreach (veical v in _vical)
                {
                    if (v is Van)
                    {
                    }
                    
                }
            }
            if (radioButton3.Checked)
            {
                foreach (veical v in _vical)
                {
                    
                }
            }
            if (radioButton4.Checked)
            {
                foreach (veical v in _vical)
                {

                }
            }
        }



sorry spelling is bad
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14243
  • View blog
  • Posts: 57,137
  • Joined: 12-June 08

Re: sorting objects in list

Posted 12 May 2011 - 07:17 AM

So... where's your sort code at? I would still need the help with answering the questions I had. I am not following what you are asking for - I guess the spelling is too far off to decipher.
Was This Post Helpful? 0
  • +
  • -

#5 Psyguy   User is offline

  • D.I.C Regular
  • member icon

Reputation: 84
  • View blog
  • Posts: 365
  • Joined: 12-January 11

Re: sorting objects in list

Posted 12 May 2011 - 08:11 AM

I'm going to go ahead and assume that "veical" is misspelled and should be "vehicle". The condition of his for each loops that contain both a car and a van are kind of a giveaway. I still don't understand what exactly the sort condition is. Some examples one might use to sort vehicles:

Price (highest to lowest)
Sold for price (highest to lowest or vice versa)
Number of miles

Before anyone can help, they are going to need that condition spelled out.

Edit: After reading your post again I am wondering if each Vehicle has a property which indicates whether it is sold or not.

This post has been edited by Psyguy: 12 May 2011 - 08:15 AM

Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: sorting objects in list

Posted 12 May 2011 - 07:22 PM

Is it your intention to duplicate all your code for each type of vehicle?
YOu seem to be setting up to repeat everything for Van, Car, or Truck depending on which radio button is clicked.
Repeating code is just bad and hard to maintain.

Instead of checking the condition of a GUI control like radioButon1.Checked you should be checking a property, that can be changed either by the radioButton or programmatically. It will give you more flexibility in the future. For example: When radioButton1 is checked then change an enumeration to CarType.Van

This way you can have a major change in how you are deciding this (like getting the data from an XML file or a database) and the rest of your code will still hold up. Otherwise you have to recode everything later for the new datasource.

Let me also throw in a couple tips:
  • You have to program as if everything breaks, nothing works, the cyberworld is not perfect, the attached hardware is flakey, the network is slow and unreliable, the harddrive is about to fail, every method will return an error and every user will do their best to break your software. Confirm everything. Range check every value. Make no assumptions or presumptions.
  • Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.
    btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e)
  • You aren't paying for variable names by the byte. So instead of variables names of a, b, c go ahead and use meaningful names like Index, TimeOut, Row, Column and so on

Was This Post Helpful? 0
  • +
  • -

#7 TommyBoy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 09-May 09

Re: sorting objects in list

Posted 13 May 2011 - 06:00 AM

From the very breif description above...

Couldn't you just use LINQ that will allow you to sorting, grouping and filtering out vehicles from your list.

Will save you doing multiple foreach loops also.

This would be a good place to start http://msdn.microsof...csharp/aa336746

Tom
Was This Post Helpful? 0
  • +
  • -

#8 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5104
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: sorting objects in list

Posted 13 May 2011 - 06:24 AM

Of course he could. From the code I'm seeing, I doubt he will actually be able to.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1