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?
10 11221 RedSon 5,000
Recognized Expert Expert
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.
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.
RedSon 5,000
Recognized Expert Expert
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.
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.
RedSon 5,000
Recognized Expert Expert
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-=--=sdlfjsdsdkjoiv nSMsdfiojg. 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.
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.
ClothingSizeSor ter.cs -
namespace Clothing_Size_Sorter
-
{
-
using System;
-
using System.Collections.Generic;
-
using System.Collections;
-
using System.Text;
-
-
public class ClothingSizeKey
-
{
-
public string[] adverbs;
-
public string delimiter;
-
public string size;
-
public int value;
-
}
-
-
public class ClothingSizeSorter
-
{
-
public string[] small_keys;
-
public string[] medium_keys;
-
public string[] large_keys;
-
public string[] adverbs;
-
public string adverb_delimiters;
-
-
public ClothingSizeSorter(string small_keys, string medium_keys, string large_keys, string adverbs, string adverb_delimiters, char key_delimiter)
-
{
-
this.small_keys = small_keys.Split(key_delimiter);
-
this.medium_keys = medium_keys.Split(key_delimiter);
-
this.large_keys = large_keys.Split(key_delimiter);
-
this.adverbs = adverbs.Split(key_delimiter);
-
this.adverb_delimiters = adverb_delimiters;
-
}
-
-
public int CompareSizeKeysByValue(ClothingSizeKey x, ClothingSizeKey y)
-
{
-
if (x == null)
-
{
-
if (y == null)
-
{
-
// If x is null and y is null, they're
-
// equal.
-
return 0;
-
}
-
else
-
{
-
// If x is null and y is not null, y
-
// is greater.
-
return -1;
-
}
-
}
-
else
-
{
-
// If x is not null...
-
//
-
if (y == null)
-
// ...and y is null, x is greater.
-
{
-
return 1;
-
}
-
else
-
{
-
int retval = x.value.CompareTo(y.value);
-
-
if (retval != 0)
-
{
-
return retval;
-
}
-
else
-
{
-
if (x.value < 0)
-
{
-
-
return Array.IndexOf<string>(small_keys, x.size).CompareTo(Array.IndexOf<string>(small_keys, y.size));
-
}
-
else if (x.value > 0)
-
{
-
return Array.IndexOf<string>(large_keys, x.size).CompareTo(Array.IndexOf<string>(large_keys, y.size));
-
}
-
else
-
{
-
return Array.IndexOf<string>(medium_keys, x.size).CompareTo(Array.IndexOf<string>(medium_keys, y.size));
-
}
-
}
-
}
-
}
-
}
-
-
public void Sort(string sizes, out string output, char delimiter)
-
{
-
string[] out_str;
-
Sort(sizes.Split(delimiter), out out_str);
-
-
output = "";
-
for (int i = 0; i < out_str.Length; i++)
-
{
-
output += out_str[i];
-
if (i + 1 < out_str.Length)
-
output += delimiter;
-
}
-
}
-
-
public void Sort(string[] sizes, out string[] output)
-
{
-
int i, x;
-
List<ClothingSizeKey> size_keys = new List<ClothingSizeKey>();
-
ClothingSizeKey size_key;
-
string[] size_key_parts;
-
string key_adverbs;
-
List<string> sorted_sizes = new List<string>();
-
string size_str;
-
int last_adverb_delimiter;
-
-
if (sizes.Length < 2)
-
{
-
output = sizes;
-
return;
-
}
-
-
// interpret each size key in sizes and store it in size_keys
-
for (i = 0; i < sizes.Length; i++)
-
{
-
size_key = new ClothingSizeKey();
-
size_key.adverbs = new string[0];
-
key_adverbs = "";
-
-
// extract the key parts
-
if ((adverb_delimiters != null) && (adverb_delimiters.Length > 0))
-
{
-
// split by delimiter (get last index)
-
last_adverb_delimiter = sizes[i].LastIndexOfAny(adverb_delimiters.ToCharArray());
-
-
if (last_adverb_delimiter >= 0)
-
{
-
key_adverbs = sizes[i].Substring(0, last_adverb_delimiter);
-
size_key.size = sizes[i].Substring(last_adverb_delimiter + 1);
-
size_key.delimiter = sizes[i][last_adverb_delimiter].ToString();
-
}
-
else
-
{
-
// no delimiters so split by adverb
-
size_key.delimiter = "";
-
}
-
}
-
-
if (size_key.delimiter.Length == 0)
-
{
-
size_key_parts = sizes[i].Split(adverbs, StringSplitOptions.RemoveEmptyEntries);
-
-
size_key.size = size_key_parts[0];
-
key_adverbs = sizes[i].Replace(size_key.size, "");
-
size_key.delimiter = "";
-
}
-
-
// separate repeating adverbs
-
-
x = 0;
-
while (x < key_adverbs.Length)
-
{
-
foreach (string adverb in adverbs)
-
{
-
if (key_adverbs.Substring(x).StartsWith(adverb))
-
{
-
Array.Resize<string>(ref size_key.adverbs, size_key.adverbs.Length + 1);
-
size_key.adverbs[size_key.adverbs.Length - 1] = adverb;
-
x += adverb.Length;
-
-
//if ((x < key_adverbs.Length) && (size_key.delimiter.IndexOf(key_adverbs[x]) >= 0))
-
if ((x < key_adverbs.Length) && (adverb_delimiters.IndexOf(key_adverbs[x]) >= 0))
-
{
-
size_key.adverbs[size_key.adverbs.Length - 1] += key_adverbs[x].ToString();
-
x++;
-
}
-
}
-
}
-
if (x == 0)
-
{
-
// Bad delimiter
-
output = null;
-
return;
-
}
-
}
-
-
size_keys.Add(size_key);
-
}
-
-
// assign size key values
-
for (i = 0; i < size_keys.Count; i++)
-
{
-
if (Array.IndexOf<string>(small_keys, size_keys[i].size) >= 0)
-
{
-
x = -10;
-
}
-
else if (Array.IndexOf<string>(large_keys, size_keys[i].size) >= 0)
-
{
-
x = 10;
-
}
-
else
-
{
-
x = 0;
-
}
-
-
size_keys[i].value = x + (x * size_keys[i].adverbs.Length);
-
}
-
-
// sort
-
size_keys.Sort(CompareSizeKeysByValue);
-
-
// build sorted values array
-
for (i = 0; i < size_keys.Count; i++)
-
{
-
size_str = "";
-
if (size_keys[i].adverbs.Length > 0)
-
{
-
for (x = 0; x < size_keys[i].adverbs.Length; x++)
-
{
-
size_str += size_keys[i].adverbs[x];
-
}
-
}
-
if (size_keys[i].delimiter.Length > 0)
-
{
-
size_str += size_keys[i].delimiter;
-
}
-
size_str += size_keys[i].size;
-
-
sorted_sizes.Add(size_str);
-
}
-
-
output = sorted_sizes.ToArray();
-
}
-
}
-
}
-
Program.cs -
using System;
-
using System.Collections.Generic;
-
using System.Collections;
-
using System.Text;
-
using Clothing_Size_Sorter;
-
-
namespace ClothingSort
-
{
-
class Program
-
{
-
static void Main(string[] args)
-
{
-
string spaced_input_sizes;
-
string dotted_input_sizes;
-
string sorted_sizes;
-
string[] input_sizes_array;
-
string[] sorted_sizes_array;
-
-
// Space Delimited Sizes
-
//spaced_input_sizes = "XS S XL M XXS L XXL";
-
spaced_input_sizes = "X-S S X-L M XX-S L XX-L";
-
//spaced_input_sizes = "EXTRA-SMALL SMALL EXTRA-LARGE MEDIUM EXTRAEXTRA-SMALL LARGE EXTRAEXTRA-LARGE";
-
//spaced_input_sizes = "EXTRA-SMALL SMALL EXTRA-LARGE MEDIUM EXTRA-EXTRA-SMALL LARGE EXTRA-EXTRA-LARGE";
-
//spaced_input_sizes = "EXTRA-SMALL SMALL EXTRA-LARGE MEDIUM EXTRA*EXTRA-SMALL LARGE EXTRA*EXTRA-LARGE";
-
-
// Dot Delimited Sizes
-
dotted_input_sizes = "EX SMALL.SMALL.EX LARGE.MEDIUM.EX EX SMALL.LARGE.EX EX LARGE";
-
-
// Arrayed Sizes
-
input_sizes_array = new string[] { "SUPER SMALL", "SMALL", "SUPER LARGE", "MEDIUM", "SUPER SUPER SMALL", "SUPER SUPER LARGE" };
-
-
ClothingSizeSorter css;
-
css = new ClothingSizeSorter("S SM SMALL", "M MED MEDIUM", "L LRG LARGE", "X EX EXTRA SUPER", "-* ", ' ');
-
-
// Space Delimited Sizes
-
css.Sort(spaced_input_sizes, out sorted_sizes, ' ');
-
Console.WriteLine("Space Delimited");
-
Console.WriteLine("Input: " + spaced_input_sizes);
-
Console.WriteLine("Output: " + sorted_sizes);
-
Console.WriteLine();
-
-
// Dot Delimited Sizes
-
css.Sort(dotted_input_sizes, out sorted_sizes, '.');
-
Console.WriteLine("Dot Delimited");
-
Console.WriteLine("Input: " + dotted_input_sizes);
-
Console.WriteLine("Output: " + sorted_sizes);
-
Console.WriteLine();
-
-
// Array Sizes
-
css.Sort(input_sizes_array, out sorted_sizes_array);
-
Console.WriteLine("Arrayed");
-
Console.WriteLine("Input:");
-
for (int i = 0; i < input_sizes_array.Length; i++)
-
{
-
Console.WriteLine(input_sizes_array[i]);
-
}
-
Console.WriteLine("Output:");
-
-
for (int i = 0; i < sorted_sizes_array.Length; i++)
-
{
-
Console.WriteLine(sorted_sizes_array[i]);
-
}
-
-
Console.WriteLine("Press any key to continue...");
-
Console.ReadKey(true);
-
}
-
}
-
}
-
-
Hopefully this didn't violate any rules... :)
SammyB 807
Recognized Expert Contributor
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: - Public Class Form1
-
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
-
Dim c As List(Of String)
-
c = New List(Of String)
-
' Add clothes to the list
-
c.Sort(New sortClothes)
-
End Sub
-
End Class
-
Public Class sortClothes : Implements IComparer
-
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
-
' Need code here to parse out size & compare them
-
End Function
-
End Class
For more details and a better explanation, see http://samples.gotdotnet.com/quickst.../doc/sort.aspx
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.
why not use an enum and sort by numbers?
Sign in to post your reply or Sign up for a free account.
Similar topics |
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()
sorted_feature_vector.sort()
#feature_vector.keys()=sorted_feature_vector
|
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()
|
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 234 456 57} or {a d f t b y}
For this i am using template class,now the problem is that how to
create objects of template class based on the data type entered by
|
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 function that will merge like elements into
a chain, so that they won't have to be recompared as many times?
Thanks!
|
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 = 81.25
| |
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 available sizes
and colors for that particular item.
I am not sure if I am posting in the right category. Still I am waiting
for the answers.
Best Wishes
|
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 trouble though. On lines 54-59, I previously had them placed from line 46 on. This worked out great for printing just the bubble sort, but I have to get the other four sorting algorithms in here and I was hoping to print the contents out in a for loop, as...
|
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 roles. You may be
meeting clients, or working at a desk, or going from the office to a
business event. One business outfit doesn't work for every occasion
any more.
Here are 3 tips for dressing for success in 2008
|
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 the size of input. I am
not able to come up with anyting all all and doing:
char* arr_of_pointers;
seems like a completely wrong idea as this is a static array. I want to
take the input and then decide how much memory I need and then malloc...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |