473,408 Members | 1,826 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Sorting Clothing Sizes (XXS XS S M L XL XXL)

I've been trying to find a reasonable way to sort a collection of clothing items by size but google isn't helping my out very much. I know this has to have been done before. I've seen an example written in Python but I don't do Python and the syntax is far too abstract to even sit and try to pick it apart.

The best thing I've been able to come up with is to:
1. Reverse the strings
2. Sort them reversed
3. Reverse the order
4. Reverse the strings

example set: S XL M XXS L XXL

// Reverse the strings
1. S LX M SXX L LXX

// Sort them reversed
2. L LX LXX M S SX SXX

// Reverse the order
3. SXX SX S M L LX LXX

// Reverse the strings
4. XXS XS S M L XL XXL

Can anyone come up with something faster/better?
Mar 28 '07 #1
10 11021
RedSon
5,000 Expert 4TB
You are only looking at 7 things why don't you just sort them by hard coding the values? Or even assign numbers to them as a key value pair and sort that way.
Mar 28 '07 #2
You are only looking at 7 things why don't you just sort them by hard coding the values? Or even assign numbers to them as a key value pair and sort that way.
That was just an example. In the production environment the sorting routine needs to be able to sort based on text alone. There is no indication that those will be the only values and also no rule that states the sizes will all ways appear as XS, XXS, or XL. For example, the size may come in as X-S or XSM.

Whatever the scheme, it will continue to be followed throughout the set of item sizes.

I'd like to be able to also accomodate schemes like X-SMALL or XX-SMALL as I've seen that scheme in some of the older data, but there is no guarantee that a future data set won't have that scheme or any other common clothing size scheme.

There are also situations where there my be a color or other descriptor following the size such as XL (Black). For those situations I currently split the string between the size and descriptor and process them separately which makes sense to me.
Mar 28 '07 #3
RedSon
5,000 Expert 4TB
That was just an example. In the production environment the sorting routine needs to be able to sort based on text alone. There is no indication that those will be the only values and also no rule that states the sizes will all ways appear as XS, XXS, or XL. For example, the size may come in as X-S or XSM.

Whatever the scheme, it will continue to be followed throughout the set of item sizes.

I'd like to be able to also accomodate schemes like X-SMALL or XX-SMALL as I've seen that scheme in some of the older data, but there is no guarantee that a future data set won't have that scheme or any other common clothing size scheme.

There are also situations where there my be a color or other descriptor following the size such as XL (Black). For those situations I currently split the string between the size and descriptor and process them separately which makes sense to me.
It's impossible to do this based on a text sort alone. You must have a set amount of tokens or at least a predetermined set of data. Making your tokens S M L is fine then you can parse the XS XXXXXXS and XXXXXXXXXXXS easy enough with out creating more cases to sort by, just convert the X's to a number. But sorting S X-Small XSML XBSML SMLXB DUCK CAR BOAT will never work.
Mar 28 '07 #4

Whatever the scheme, it will continue to be followed throughout the set of item sizes.
Let me spell out this statement...

... set of item sizes.
- The set of strings received will all correlate to clothing sizes; there will be no BOATs, DUCKs, BOMBs, or MAZDAs.

Whatever the scheme, it will continue to be followed throughout...
- If, in this set of strings, the size SMALL is indicated as SM, then all other sizes relating to small will also use SM; XXSM XSM SM. The set will not be mixed; there will be no XX-SMALL XS SM.

For the record... The method I first described doesn't work because the result set will have the large items reversed.

At any rate, I believe I may have come up with a workable solution. I'll post it if it works.
Mar 28 '07 #5
RedSon
5,000 Expert 4TB
I'd like to be able to also accomodate schemes like X-SMALL or XX-SMALL as I've seen that scheme in some of the older data, but there is no guarantee that a future data set won't have that scheme or any other common clothing size scheme.
This reads to me like you are mixing entries for different representations of extra small shirt. Your first post talks about XXS XS S M L then in subsequent posts you talk about X-SMALL, XSM and what not. So sorry for the confusion.

What you need to do, and I suspect you are already doing this is identify what three tokens you are going to identify as small, medium and large. SM, MED, and LG work fine for this. Your next step is to count the number of Xs that come before these tokens. This should be fairly simple to sort, convert number of Xs to an int and sort from largest to smallest since more Xs means smaller or larger dpeneding on size token. Then after you have this logic, you can add in the "ignore everything else" logic. That means that XXX-SMALL will be treated the same as XXX-=--=sdlfjsdsdkjoivnSMsdfiojg. Then after you have that you want to add the logic in that either removes all the junk characters or tells the user to "get a life" and stop entering in bogus data to try to muck up the system.
Mar 28 '07 #6
For those interested (and also those who like to find bugs)... The following is the quick and dirty solution I came up with yesterday that meets all of the needs I specified. The code is NOT fail safe and NOT checked for exceptions. It should NOT be used in a production environment, especially when the input data and size rules will not be reasonably constant. It could be heavily optimized but that should be unnecessary. Thanks RedSon, the discussion with you helped to get my mind going in the right direction.


ClothingSizeSorter.cs
Expand|Select|Wrap|Line Numbers
  1. namespace Clothing_Size_Sorter
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Collections;
  6.     using System.Text;
  7.  
  8.     public class ClothingSizeKey
  9.     {
  10.         public string[] adverbs;
  11.         public string delimiter;
  12.         public string size;
  13.         public int value;
  14.     }
  15.  
  16.     public class ClothingSizeSorter
  17.     {
  18.         public string[] small_keys;
  19.         public string[] medium_keys;
  20.         public string[] large_keys;
  21.         public string[] adverbs;
  22.         public string adverb_delimiters;
  23.  
  24.         public ClothingSizeSorter(string small_keys, string medium_keys, string large_keys, string adverbs, string adverb_delimiters, char key_delimiter)
  25.         {
  26.             this.small_keys = small_keys.Split(key_delimiter);
  27.             this.medium_keys = medium_keys.Split(key_delimiter);
  28.             this.large_keys = large_keys.Split(key_delimiter);
  29.             this.adverbs = adverbs.Split(key_delimiter);
  30.             this.adverb_delimiters = adverb_delimiters;
  31.         }
  32.  
  33.         public int CompareSizeKeysByValue(ClothingSizeKey x, ClothingSizeKey y)
  34.         {
  35.             if (x == null)
  36.             {
  37.                 if (y == null)
  38.                 {
  39.                     // If x is null and y is null, they're
  40.                     // equal. 
  41.                     return 0;
  42.                 }
  43.                 else
  44.                 {
  45.                     // If x is null and y is not null, y
  46.                     // is greater. 
  47.                     return -1;
  48.                 }
  49.             }
  50.             else
  51.             {
  52.                 // If x is not null...
  53.                 //
  54.                 if (y == null)
  55.                 // ...and y is null, x is greater.
  56.                 {
  57.                     return 1;
  58.                 }
  59.                 else
  60.                 {
  61.                     int retval = x.value.CompareTo(y.value);
  62.  
  63.                     if (retval != 0)
  64.                     {
  65.                         return retval;
  66.                     }
  67.                     else
  68.                     {
  69.                         if (x.value < 0)
  70.                         {
  71.  
  72.                             return Array.IndexOf<string>(small_keys, x.size).CompareTo(Array.IndexOf<string>(small_keys, y.size));
  73.                         }
  74.                         else if (x.value > 0)
  75.                         {
  76.                             return Array.IndexOf<string>(large_keys, x.size).CompareTo(Array.IndexOf<string>(large_keys, y.size));
  77.                         }
  78.                         else
  79.                         {
  80.                             return Array.IndexOf<string>(medium_keys, x.size).CompareTo(Array.IndexOf<string>(medium_keys, y.size));
  81.                         }
  82.                     }
  83.                 }
  84.             }
  85.         }
  86.  
  87.         public void Sort(string sizes, out string output, char delimiter)
  88.         {
  89.             string[] out_str;
  90.             Sort(sizes.Split(delimiter), out out_str);
  91.  
  92.             output = "";
  93.             for (int i = 0; i < out_str.Length; i++)
  94.             {
  95.                 output += out_str[i];
  96.                 if (i + 1 < out_str.Length)
  97.                     output += delimiter;
  98.             }
  99.         }
  100.  
  101.         public void Sort(string[] sizes, out string[] output)
  102.         {
  103.             int i, x;
  104.             List<ClothingSizeKey> size_keys = new List<ClothingSizeKey>();
  105.             ClothingSizeKey size_key;
  106.             string[] size_key_parts;
  107.             string key_adverbs;
  108.             List<string> sorted_sizes = new List<string>();
  109.             string size_str;
  110.             int last_adverb_delimiter;
  111.  
  112.             if (sizes.Length < 2)
  113.             {
  114.                 output = sizes;
  115.                 return;
  116.             }
  117.  
  118.             // interpret each size key in sizes and store it in size_keys
  119.             for (i = 0; i < sizes.Length; i++)
  120.             {
  121.                 size_key = new ClothingSizeKey();
  122.                 size_key.adverbs = new string[0];
  123.                 key_adverbs = "";
  124.  
  125.                 // extract the key parts
  126.                 if ((adverb_delimiters != null) && (adverb_delimiters.Length > 0))
  127.                 {
  128.                     // split by delimiter (get last index)
  129.                     last_adverb_delimiter = sizes[i].LastIndexOfAny(adverb_delimiters.ToCharArray());
  130.  
  131.                     if (last_adverb_delimiter >= 0)
  132.                     {
  133.                         key_adverbs = sizes[i].Substring(0, last_adverb_delimiter);
  134.                         size_key.size = sizes[i].Substring(last_adverb_delimiter + 1);
  135.                         size_key.delimiter = sizes[i][last_adverb_delimiter].ToString();
  136.                     }
  137.                     else
  138.                     {
  139.                         // no delimiters so split by adverb
  140.                         size_key.delimiter = "";
  141.                     }
  142.                 }
  143.  
  144.                 if (size_key.delimiter.Length == 0)
  145.                 {
  146.                     size_key_parts = sizes[i].Split(adverbs, StringSplitOptions.RemoveEmptyEntries);
  147.  
  148.                     size_key.size = size_key_parts[0];
  149.                     key_adverbs = sizes[i].Replace(size_key.size, "");
  150.                     size_key.delimiter = "";
  151.                 }
  152.  
  153.                 // separate repeating adverbs
  154.  
  155.                 x = 0;
  156.                 while (x < key_adverbs.Length)
  157.                 {
  158.                     foreach (string adverb in adverbs)
  159.                     {
  160.                         if (key_adverbs.Substring(x).StartsWith(adverb))
  161.                         {
  162.                             Array.Resize<string>(ref size_key.adverbs, size_key.adverbs.Length + 1);
  163.                             size_key.adverbs[size_key.adverbs.Length - 1] = adverb;
  164.                             x += adverb.Length;
  165.  
  166.                             //if ((x < key_adverbs.Length) && (size_key.delimiter.IndexOf(key_adverbs[x]) >= 0))
  167.                             if ((x < key_adverbs.Length) && (adverb_delimiters.IndexOf(key_adverbs[x]) >= 0))
  168.                             {
  169.                                 size_key.adverbs[size_key.adverbs.Length - 1] += key_adverbs[x].ToString();
  170.                                 x++;
  171.                             }
  172.                         }
  173.                     }
  174.                     if (x == 0)
  175.                     {
  176.                         // Bad delimiter
  177.                         output = null;
  178.                         return;
  179.                     }
  180.                 }
  181.  
  182.                 size_keys.Add(size_key);
  183.             }
  184.  
  185.             // assign size key values
  186.             for (i = 0; i < size_keys.Count; i++)
  187.             {
  188.                 if (Array.IndexOf<string>(small_keys, size_keys[i].size) >= 0)
  189.                 {
  190.                     x = -10;
  191.                 }
  192.                 else if (Array.IndexOf<string>(large_keys, size_keys[i].size) >= 0)
  193.                 {
  194.                     x = 10;
  195.                 }
  196.                 else
  197.                 {
  198.                     x = 0;
  199.                 }
  200.  
  201.                 size_keys[i].value = x + (x * size_keys[i].adverbs.Length);
  202.             }
  203.  
  204.             // sort
  205.             size_keys.Sort(CompareSizeKeysByValue);
  206.  
  207.             // build sorted values array
  208.             for (i = 0; i < size_keys.Count; i++)
  209.             {
  210.                 size_str = "";
  211.                 if (size_keys[i].adverbs.Length > 0)
  212.                 {
  213.                     for (x = 0; x < size_keys[i].adverbs.Length; x++)
  214.                     {
  215.                         size_str += size_keys[i].adverbs[x];
  216.                     }
  217.                 }
  218.                 if (size_keys[i].delimiter.Length > 0)
  219.                 {
  220.                     size_str += size_keys[i].delimiter;
  221.                 }
  222.                 size_str += size_keys[i].size;
  223.  
  224.                 sorted_sizes.Add(size_str);
  225.             }
  226.  
  227.             output = sorted_sizes.ToArray();
  228.         }
  229.     }
  230. }
  231.  
Program.cs
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4. using System.Text;
  5. using Clothing_Size_Sorter;
  6.  
  7. namespace ClothingSort
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             string spaced_input_sizes;
  14.             string dotted_input_sizes;
  15.             string sorted_sizes;
  16.             string[] input_sizes_array;
  17.             string[] sorted_sizes_array;
  18.  
  19.             // Space Delimited Sizes
  20.             //spaced_input_sizes = "XS S XL M XXS L XXL";
  21.             spaced_input_sizes = "X-S S X-L M XX-S L XX-L";
  22.             //spaced_input_sizes = "EXTRA-SMALL SMALL EXTRA-LARGE MEDIUM EXTRAEXTRA-SMALL LARGE EXTRAEXTRA-LARGE";
  23.             //spaced_input_sizes = "EXTRA-SMALL SMALL EXTRA-LARGE MEDIUM EXTRA-EXTRA-SMALL LARGE EXTRA-EXTRA-LARGE";
  24.             //spaced_input_sizes = "EXTRA-SMALL SMALL EXTRA-LARGE MEDIUM EXTRA*EXTRA-SMALL LARGE EXTRA*EXTRA-LARGE";
  25.  
  26.             // Dot Delimited Sizes
  27.             dotted_input_sizes = "EX SMALL.SMALL.EX LARGE.MEDIUM.EX EX SMALL.LARGE.EX EX LARGE";
  28.  
  29.             // Arrayed Sizes
  30.             input_sizes_array = new string[] { "SUPER SMALL", "SMALL", "SUPER LARGE", "MEDIUM", "SUPER SUPER SMALL", "SUPER SUPER LARGE" };
  31.  
  32.             ClothingSizeSorter css;
  33.             css = new ClothingSizeSorter("S SM SMALL", "M MED MEDIUM", "L LRG LARGE", "X EX EXTRA SUPER", "-* ", ' ');
  34.  
  35.             // Space Delimited Sizes
  36.             css.Sort(spaced_input_sizes, out sorted_sizes, ' ');
  37.             Console.WriteLine("Space Delimited");
  38.             Console.WriteLine("Input: " + spaced_input_sizes);
  39.             Console.WriteLine("Output: " + sorted_sizes);
  40.             Console.WriteLine();
  41.  
  42.             // Dot Delimited Sizes
  43.             css.Sort(dotted_input_sizes, out sorted_sizes, '.');
  44.             Console.WriteLine("Dot Delimited");
  45.             Console.WriteLine("Input: " + dotted_input_sizes);
  46.             Console.WriteLine("Output: " + sorted_sizes);
  47.             Console.WriteLine();
  48.  
  49.             // Array Sizes
  50.             css.Sort(input_sizes_array, out sorted_sizes_array);
  51.             Console.WriteLine("Arrayed");
  52.             Console.WriteLine("Input:");
  53.             for (int i = 0; i < input_sizes_array.Length; i++)
  54.             {
  55.                 Console.WriteLine(input_sizes_array[i]);
  56.             }
  57.             Console.WriteLine("Output:");
  58.  
  59.             for (int i = 0; i < sorted_sizes_array.Length; i++)
  60.             {
  61.                 Console.WriteLine(sorted_sizes_array[i]);
  62.             }
  63.  
  64.             Console.WriteLine("Press any key to continue...");
  65.             Console.ReadKey(true);
  66.         }
  67.     }
  68. }
  69.  
  70.  
Hopefully this didn't violate any rules... :)
Mar 29 '07 #7
SammyB
807 Expert 512MB
Hopefully this didn't violate any rules... :)
Yikes!! It only violates the rule of not reinventing the wheel. You really only need to write your own IComparer Interface and then let VB.NET use it to sort the items:
Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  3.         Dim c As List(Of String)
  4.         c = New List(Of String)
  5.         ' Add clothes to the list
  6.         c.Sort(New sortClothes)
  7.     End Sub
  8. End Class
  9. Public Class sortClothes : Implements IComparer
  10.     Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
  11.         ' Need code here to parse out size & compare them
  12.     End Function
  13. End Class
For more details and a better explanation, see http://samples.gotdotnet.com/quickst.../doc/sort.aspx
Mar 29 '07 #8
I thought of that at one point but I could not find a way to allow the code to specify valid size tokens using that method. The sorter itself needs its own class to allow rules.
Mar 30 '07 #9
why not use an enum and sort by numbers?
Apr 2 '07 #10
RedSon
5,000 Expert 4TB
why not use an enum and sort by numbers?
I think the point of not using an enum would be that it would limit the number of mappings from strings to numbers. The program would then not accept both XSM and X-SMALL.
Apr 3 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: dont bother | last post by:
This is really driving me crazy. I have a dictionary feature_vectors{}. I try to sort its keys using #apply sorting on feature_vectors sorted_feature_vector=feature_vectors.keys()...
3
by: Derek Basch | last post by:
Hello All, I need to sort a list using an unnatural sequence. I have a list like so: foo = print foo.sort()
1
by: spider | last post by:
Hi Guys I am planning to design a sorting class which will sort different data types like int ,float,double,char User will be given option Enter the data: So he can enter any data like {1...
25
by: Dan Stromberg | last post by:
Hi folks. Python appears to have a good sort method, but when sorting array elements that are very large, and hence have very expensive compares, is there some sort of already-available sort...
60
by: deko | last post by:
As I understand it, most browser manufacturers have agreed on 16px for their default font size. So, this should be an accurate conversion for percentages: px % 16 = 100 14 = 87.5 13 =...
2
by: lovely_angel_for_you | last post by:
Hi, I am looking for an ASP based online shopping cart for my upcoming clothing website. Of all the features it should have 2 main features as in Size and Color. It should allow me to specify the...
7
beacon
by: beacon | last post by:
I'm writing a program as an assignment that takes 5 sorting algorithms and and tests for the amount of time and the number of comparisons it takes to um, sort an array. I have run into some...
0
by: watches0958 | last post by:
With today's fashion trends in women's clothing, it can take more effort to dress for success every day, than to get the job in the first place. When you're working every day, you have different...
77
by: arnuld | last post by:
1st I think of creating an array of pointers of size 100 as this is the maximum input I intend to take. I can create a fixed size array but in the end I want my array to expand at run-time to fit...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.