开发者

Silverlight listbox orderby

Is there a way to order this silverlight bound ListBox. I am not sure the way I have populated makes it easy?

Code sample here:

  XElement _xml = XElement.Load("MSA.xml");
        {
            msa.Items.Clear();

            foreach (XElement value in _xml.Elements("channel").Elements("msa"))
            {
                MSAFeedItem _item = new MSAFeedItem();
                _item.Lat = double.Parse(value.Element("lat").Value);
                _item.Long = double.Parse(value.Element("long").Value);
                _item.Name = value.Element("n").Value;


 double dis1 = _item.Lat - curLatitude;
                        double dis2 = _item.Long - curLongitude;

                     var miles1 = Math.Pow(dis1, 2.0);
                     var miles2 = Math.Pow(dis2, 2.0);

               开发者_运维百科      var miles3 = miles1 + miles2;

                     var miles4 = Math.Sqrt(miles3) ;

                     var miles = miles4 * 62.1371192;

                    _item.Distance = Decimal.Truncate(Convert.ToDecimal(miles));

                 msa.Items.Add(_item);

            }
        }

It needs to be in ascending order according to a double or decimal value called 'miles' ListBox is called 'msa'

Many thanks for any help or suggestions.

EDIT: Ive added my distance code to help you understand whats going on, so _item.distance or 'miles' is the number it needs to ascend by. The code is a bit messy and doesnt work exactly rightt but you get the idea.


The easiest way to go here is to use a list, which binds to the listbox:

        var elements = new List<MSAFeedItem>();
        var comparison = new Comparison<MSAFeedItem>(
            (x, y) => {
                if (x.Miles < y.Miles) return -1;
                else if (x.Miles > y.Miles) return 1;
                else return 0;
            });
        foreach (XElement value in _xml.Elements("channel").Elements("msa")) 
        { 
            MSAFeedItem _item = new MSAFeedItem(); 
            _item.Lat = double.Parse(value.Element("lat").Value);
            _item.Long = double.Parse(value.Element("long").Value); 
            _item.Name = value.Element("n").Value; 
            _item.Distance = Decimal.Truncate(Convert.ToDecimal(miles)); 
            elements.Add(_item); 
        }

        elements.Sort(comparison);

        msa.ItemsSource = elements;

Obviously replacing the Miles property on the X and Y items with the way you calculate the amount of miles.


 XElement _xml = XElement.Load("MSA.xml");
        {
            msa.Items.Clear();
            List<MSAFeedItem> tempItems = new List<MSAFeedItem>();

            foreach (XElement value in _xml.Elements("channel").Elements("msa"))
            {
                MSAFeedItem _item = new MSAFeedItem();
                _item.Lat = double.Parse(value.Element("lat").Value);
                _item.Long = double.Parse(value.Element("long").Value);
                _item.Name = value.Element("n").Value;


                double dis1 = _item.Lat - curLatitude;
                double dis2 = _item.Long - curLongitude;

                     var miles1 = Math.Pow(dis1, 2.0);
                     var miles2 = Math.Pow(dis2, 2.0);

                     var miles3 = miles1 + miles2;

                     var miles4 = Math.Sqrt(miles3) ;

                     var miles = miles4 * 62.1371192;

                    _item.Distance = Decimal.Truncate(Convert.ToDecimal(miles));

                 tempItems.Add(_item);

            }

           tempItems.OrderBy( i => i.Distance).ToList()
                    .ForEach(e => msa.Items.Add(e));

        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜