Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I'm having an issue sorting data in ascending order.

Right now the sort order is:
1.0.0
10.0.0
10.0.1
11.0.0
2.0.0
2.0.1
3.0.0
3.0.1
3.1.0

What I want is:
1.0.0
2.0.0
2.0.1
3.0.0
3.0.1
3.1.0
10.0.0
10.0.1
11.0.0

How can I achieve this? The datatype can't be number or currency because of the 2 decimals, so I used text-field.

share|improve this question
    
I assume this is a versioning field, previously we have included the preceding zeros for simplicity to allow correct SharePoint sorting. –  Russell Jan 13 '12 at 1:37
    
Please edit your question to include more information about what you're trying to do.. that way, we can provide you with a better answer! –  Kit Menke Jan 13 '12 at 14:26
    
I have this data in a SharePoint list and I just want to sort by it in ascending order. A few users have answered my question below. –  daver306 Jan 13 '12 at 17:02
add comment

5 Answers

up vote 3 down vote accepted

Since you are using strings as the data type, you need to format the data differently so that they sort correctly based on alphabetical order, ie

01.0.0
02.0.0
02.0.1
03.0.0
03.0.1
03.1.0
10.0.0
10.0.1
11.0.0
share|improve this answer
add comment

You can write values as 00.00.000 (eg. 01.11.345). Not the best solution but the easiest one.

More complex one is to write your own Custom Field Type.

Also take a look at SharePoint MaskedEdit Field Control and Input-Masking in SharePoint Forms. Not exactly what are you looking for but just as idea (never tested it myself).

share|improve this answer
add comment

Your data is formatted as x.y.z, and you need 3 ordering rules: x ascending, then y ascending, then z ascending. You could use 3 columns to store x, y and z. You would apply the sorting criteria, and for rendering you would use a calculated column: =x+"."+y+"."+z

If it's too many columns to handle, then you would have to use the 00.00.000 format suggested by Vedran. If you don't want to do this manually, you would again use a calculated column that will do the formatting (definitely not a simple formula).

share|improve this answer
    
Thanks for suggesting the approach @Christophe. I modified your suggestion to X&"."&Y&"."&Z since & is the SharePoint aggregator, and then sorted based on my aggregated column, since SP only allows two columns to specify sort. –  jg100309 Oct 15 '13 at 18:35
add comment

DISCLAIMER: NOt sure if code based solution would work for the user as it is unclear to me where exactly it is being used. But if user has opportunity to write code to sort the versions, following algorithm is strong and will work without any workarounds like prefixing with 0 etc.

ANSWER:

You just need to write a custom comparer and sort it using custom comparer. Below is the logic to convert version of above format into a integer value and use the underlying integer value to sort it.

This is a console app which I just wrote but you get the idea! (Note that I have not put the validation, always assume right input)

class Program
    {
        public class VersionComparer : IComparer<string>
        {
            public int Compare(string x, string y)
            {
                string[] xVersionParts = x.Split('.');
                string[] yVersionParts = y.Split('.');
                if (xVersionParts[0] != yVersionParts[0])
                return int.Parse(xVersionParts[0]).CompareTo(int.Parse(yVersionParts[0]));
            else if (xVersionParts[1] != yVersionParts[1])
                return int.Parse(xVersionParts[1]).CompareTo(int.Parse(yVersionParts[1]));
            else
                return int.Parse(xVersionParts[2]).CompareTo(int.Parse(yVersionParts[2]));
            }
        }
        static void Main(string[] args)
        {
            List<string> versions = new List<string>();
            versions.Add("1.0.0");
            versions.Add("10.0.0");
            versions.Add("10.0.1");
            versions.Add("11.0.0");
            versions.Add("2.0.0");
            versions.Add("2.0.1");
            versions.Add("3.0.0");
            versions.Add("3.1.0");            
            versions.Add("3.0.1");

            versions.Sort();
            Console.WriteLine("Before Custom Comparer...");
            foreach (string version in versions)
            {
                Console.WriteLine(version);
            }


            versions.Sort(new VersionComparer());
            Console.WriteLine("After Custom Comparer...");
            foreach (string version in versions)
            {
                Console.WriteLine(version);
            }

            Console.ReadLine();
        }
    }

Here are the results....

enter image description here

share|improve this answer
2  
-1 This is obviously simple SharePoint list ordering question and you are providing complex console application. How and where exactly can you implement this code? –  Vedran Rasol Jan 13 '12 at 1:01
    
there is no details provided as to where exactly user is displaying it. Besides, if it is sharepoint's OOB functionality, it is smart enough to put it in correct order. Whether the app is complex or not depends on reader's skills. –  Ashish Patel Jan 13 '12 at 1:11
1  
Isn't tag of this question 'list' and I really don't see any code request in question. Don't get me wrong - you have nice little code snippet here but in context of this question is a bit confusing and definitely a overkill. What your answer really needs is some sort of 'disclaimer' like 'If you are looking for code solution....' –  Vedran Rasol Jan 13 '12 at 1:45
    
your suggestion accepted and disclaimer added. –  Ashish Patel Jan 13 '12 at 3:20
    
and I removed my downvote –  Vedran Rasol Jan 13 '12 at 9:28
add comment

Paul's solution is the way to go but sometimes changing your data format just for sorting just isn't in the cards. If that's the case, implement a new column in your list that you can use to sort on. It's a little hacky but you gotta do what you gotta do.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.