简体   繁体   中英

How to populate a listbox sorted with unsorted array

I have an array of items with two properties, name and position. The array is not sorted in any way and I want to populate a listbox in order of position.

I can do it with this code down below, first I add all items so I have a list with correct numbers of items and then replace the items at correct position. But I wonder if there is a better way to do it?

I want the listbox to have the names in this order: Mark, John and James.

Note: the James, Mark and John data is just an example, and I can't sort the standing array.

public class _standing
{
   public _standing(string _name, int _pos) {
      name = _name;
      position = _pos;
   }
   public string name { get; set; }
   public int position { get; set; }
}

_standing a = new _standing("James", 2);
_standing b = new _standing("Mark", 0);
_standing c = new _standing("John", 1);
_standing[] standing = new _standing[]{a, b, c};

for (int i = 0; i < standing.Length; i++) {
   listBox1.Items.Add(standing[i].name);
}
for (int i = 0; i < standing.Length; i++) {
   listBox1.Items.RemoveAt(standing[i].position);
   listBox1.Items.Insert(standing[i].position, standing[i].name);
}

You can just use the OrderBy method of the array:

standing = standing.OrderBy(i => i.position).ToArray();
listBox1.Items.AddRange(standing);

You can also order by decscending:

standing.OrderByDescending(i => i.position).ToArray();

These both require a reference to System.Linq

Also, since OrderBy returns a new object, you can also do this without re-ordering your original list:

_standing a = new _standing("James", 2);
_standing b = new _standing("Mark", 0);
_standing c = new _standing("John", 1);
_standing[] standing = new _standing[] { a, b, c };

listBox1.Items.AddRange(standing.OrderBy(i => i.position).ToArray());

Update

In order to show something meaningful in your listBox1, you should override the ToString method on your _standing class, something like:

public class _standing
{
    public string name { get; set; }
    public int position { get; set; }

    public _standing(string _name, int _pos)
    {
        name = _name;
        position = _pos;
    }

    public override string ToString()
    {
        return position + ": " + name;
    }
}

Finally, I have to mention that your casing/naming conventions are not standard C#. The standard is for Classes and Properties to be PascalCase, for arguments to be camelCase, and for private fields to be pascalCase with an optional underscore prefix. So your code would ideally look something like:

public class Standing
{
    public string Name { get; set; }
    public int Position { get; set; }

    public Standing(string name, int position)
    {
        Name = name;
        Position = position;
    }

    public override string ToString()
    {
        return Position + ": " + Name;
    }
}

and...

Standing a = new Standing("James", 2);
Standing b = new Standing("Mark", 0);
Standing c = new Standing("John", 1);
Standing[] standing = { a, b, c };

listBox1.Items.AddRange(standing.OrderBy(i => i.Position).ToArray());

If I were you I would create a list of _standing first and add to the list. Something like:

List<_standing> standingList = new List<_standing>();
standingList.Add(new _standing("James", 2)); 
etc...

The advantage of using a List over the array type is that it's size is dynamic. If you are only ever going to have 3 _standing objects then an array is fine, but in reality that is probably unlikely to be the case.

Then you can use a Linq query to sort the list by position

IEnumerable<_standing> sorted = standingList.OrderBy(stand => stand.Position);

You now have a sorted List using .NET built in sorting algorithms and you can add this to your control Items collection. You can save time by using the AddRange method:

listBox1.Items.AddRange(sorted);

Sources for reference:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM