Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7543039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:19:28+00:00 2026-05-30T08:19:28+00:00

I have a List<Unit> where Unit contains Name and Value . In this object

  • 0

I have a List<Unit> where Unit contains Name and Value. In this object I store information about apparel sizes Name contains size names (S,M,L,XL..) and Value contains the quantity of that size.

This unit list is contained from a database, but the list comes in random order, so in the liste it might be like this:

Unit(M,3)
Unit(S,1)
Unit(XXL,2)
Unit(L,2)

I would like to sort the list so that it become more like this:

Unit(S,1)
Unit(M,3)
Unit(L,2)
Unit(XXLL,2)

I cant order on the string ASCE or DESC since it M comes before S and so on.
Then I thought I might create an reference Array with the correct order (XXS,XS,S,M,L,XL,XXL,XXXL), but how can I sort my list according to the reference.

Or are there other clever ways of doing this?

Update

Thanks for all good answers, I landed on the Enum solution, and it finally looks like this:

public class Unit
{
    public Unit()
    {
    }

    public Unit(string name, int value)
    {
        Value = value;
        SizeCode = AssignSizeCode(name);
    }

    public SizeCode SizeCode { get; set; }
    public int Value { get; set; }

    private SizeCode AssignSizeCode(string name)
    {
        switch (name)
        {
            case "XXS":
                return SizeCode.XXS;
            case "XS":
                return SizeCode.XS;
            case "S":
                return SizeCode.S;
            case "M":
                return SizeCode.M;
            case "L":
                return SizeCode.L;
            case "XL":
                return SizeCode.XL;
            case "XXL":
                return SizeCode.XXL;
            case "XXXL":
                return SizeCode.XXXL;
            default:
                return SizeCode.Unknown;
        }
    }

}

public enum SizeCode
{
    XXS = 1,
    XS = 2,
    S = 3,
    M = 4,
    L = 5,
    XL = 6,
    XXL = 7,
    XXXL = 8,
    Unknown = 9
}

And I sort it like this:

units = units.OrderBy(x => (int)x.SizeCode).ToList();

Any comments, or things I can improve?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T08:19:30+00:00Added an answer on May 30, 2026 at 8:19 am

    How about using a enum

    public enum Size
        {
            Small = 1,
            Medium = 2,
            // etc 
    
        }
    

    Then you can convert the enum value in Unit class to int and sort by the integer value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have this xml file <?xml version=1.0 encoding=utf-8?> <CRM> <Unit ID=1 Name=آذربایجان شرقی> <city
I have List objects which are shown like this: www.mysite.com/lists/123 Where 123 is the
I have list in python which has following entries name-1 name-2 name-3 name-4 name-1
I have a list: list<Unit *> UnitCollection; containing Unit objects, which has an accessor
I have list of elements and when I choose element from list, "value" from
I have a List of CartItem , some of which have identical Name properties.
I have long file with the following list: /drivers/isdn/hardware/eicon/message.c//add_b1() /drivers/media/video/saa7134/saa7134-dvb.c//dvb_init() /sound/pci/ac97/ac97_codec.c//snd_ac97_mixer_build() /drivers/s390/char/tape_34xx.c//tape_34xx_unit_check() (PROBLEM)/drivers/video/sis/init301.c//SiS_GetCRT2Data301() /drivers/scsi/sg.c//sg_ioctl()
I have List I want to sort Desc by Priority, which is int and
i have list of rows that user select and i want to delete them,
i have list of images and on mouse over there is option box shows

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.