Results 1 to 3 of 3

Thread: [2.0] File Version Sorting.

  1. #1

    Thread Starter
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    [2.0] File Version Sorting.

    Hi Guys good day. What is the best way to sort a file version?

    ie
    4.06.045.07
    3.06.045.07
    4.12.45.074
    4.06.04.207

    so it would be this order:

    3.06.045.07
    4.06.04.207
    4.06.045.07
    4.12.45.074

    Any inputs are much appreciated.

    thanks.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] File Version Sorting.

    Create Version objects and create an IComparer to compare them:
    Code:
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                List<Version> versionList = new List<Version>();
    
                versionList.Add(new Version("4.06.045.07"));
                versionList.Add(new Version("3.06.045.07"));
                versionList.Add(new Version("4.12.45.074"));
                versionList.Add(new Version("4.06.04.207"));
    
                versionList.Sort(new VersionComparer());
    
                foreach (Version ver in versionList)
                {
                    MessageBox.Show(ver.ToString());
                }
            }
        }
    
        public class VersionComparer : IComparer<Version>
        {
            int IComparer<Version>.Compare(Version x, Version y)
            {
                int result = x.Major - y.Major;
    
                if (result == 0)
                {
                    result = x.Minor - y.Minor;
                }
    
                if (result == 0)
                {
                    result = x.Build - y.Build;
                }
    
                if (result == 0)
                {
                    result = x.Revision - y.Revision;
                }
    
                return result;
            }
        }
    Note that you can alter the way the versions are compared if you want but 045 is 45 and that's how it will be treated because version components and numbers and leading zeroes on numbers are meaningless. If you want to keep your leading zeroes then Version.ToString won't do it and this IComparer will ignore them in any comparisons. You could define your own version class if you wanted, or you you could just make your IComparer more complex and have it break up the string internally.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: [2.0] File Version Sorting.

    Thanks JM.

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