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 7956093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:53:33+00:00 2026-06-04T03:53:33+00:00

public class foo { int ID { get; set; } byte[] sort { get;

  • 0
public class foo {
  int ID { get; set; }
  byte[] sort { get; set; }
}

public class barMaster {
    public void FooSource() {
        return List<foo> FromDataSource;
    }
    public void display() {
        List<foo> sortedFoo = FooSource().OrderBy(f => f.sort);
        UIElement = sortedFoo;
    }

I have a set of objects that contain a byte[] property that I want to OrderBy, however, OrderBy(byte[]) throws an error:

System.ArgumentException: At least one object must implement IComparable.

What can I do to OrderBy byte[] values?

  • 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-06-04T03:53:34+00:00Added an answer on June 4, 2026 at 3:53 am

    As you’ve indicated that the arrays are of variable length (as it’s a SQL Server hierarchy ID), you absolutely need to create a custom IComparer<byte[]> implementation.

    The logic is simple:

    • Compare the first n bytes of each array byte-for-byte, where n is the number of bytes in the smaller of the two arrays. When a difference is detected between any byte, return the result of the comparison of the different bytes.
    • If the first n bytes are equal, return the comparison of the lengths of the two arrays.

    This way, given a set of data like so:

    00 01 02
    00 01
    01
    

    When sorted, the results you’ll get are:

    00 01
    00 01 02
    01
    

    That said, this is what your IComparer<byte[]> implementation will look like:

    // I could be wrong in that this is called natural order.
    class NaturalOrderByteArrayComparer : IComparer<byte[]>
    {
        public int Compare(byte[] x, byte[] y)
        {
            // Shortcuts: If both are null, they are the same.
            if (x == null && y == null) return 0;
    
            // If one is null and the other isn't, then the
            // one that is null is "lesser".
            if (x == null && y != null) return -1;
            if (x != null && y == null) return 1;
    
            // Both arrays are non-null.  Find the shorter
            // of the two lengths.
            int bytesToCompare = Math.Min(x.Length, y.Length);
    
            // Compare the bytes.
            for (int index = 0; index < bytesToCompare; ++index)
            {
                // The x and y bytes.
                byte xByte = x[index];
                byte yByte = y[index];
    
                // Compare result.
                int compareResult = Comparer<byte>.Default.Compare(xByte, yByte);
    
                // If not the same, then return the result of the
                // comparison of the bytes, as they were the same
                // up until now.
                if (compareResult != 0) return compareResult;
    
                // They are the same, continue.
            }
    
            // The first n bytes are the same.  Compare lengths.
            // If the lengths are the same, the arrays
            // are the same.
            if (x.Length == y.Length) return 0;
    
            // Compare lengths.
            return x.Length < y.Length ? -1 : 1;
        }
    }
    

    As an aside, if your byte arrays were guaranteed to be the same length, as an alternative you can dynamically create the order by clause, sorting by the first element, then the second, etc, etc, like so:

    static IEnumerable<foo> OrderBySortField(this IEnumerable<foo> items, 
        int sortLength)
    {
        // Validate parameters.
        if (items == null) throw new ArgumentNullException("items");
        if (sortLength < 0) throw 
            new ArgumentOutOfRangeException("sortLength", sortLength,
                "The sortLength parameter must be a non-negative value.");
    
        // Shortcut, if sortLength is zero, return the sequence, as-is.
        if (sortLength == 0) return items;
    
        // The ordered enumerable.
        IOrderedEnumerable<foo> ordered = items.OrderBy(i => i.sort[0]);
    
        // Cycle from the second index on.
        for (int index = 1; index < sortLength; index++)
        {
            // Copy the index.
            int indexCopy = index;
    
            // Sort by the next item in the array.
            ordered = ordered.ThenBy(i => i.sort[indexCopy]);
        }
    
        // Return the ordered enumerable.
        return ordered;
    }
    

    And then you can simply call it like so:

    // You have to supply the length of the array you're sorting on.
    List<foo> sortedFoo = FooSource().
        OrderBySortField(sortLength).ToList();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

class Foo { public int A { get; set; } } class Program {
[DataContract] public class Foo { [DataMember(Order = 1)] public int FooId { get; set;
I have the a class Foo like this: class Foo { public int id{get;set;}
I have two classes: public class Foo { public int FooId {get;set;} public virtual
I have the following: public class Foo { public int x { get; set;
I have two classes: class Foo{ public int FooId { get; set; } ...
Given the following class public class Foo { public int FooId { get; set;
Given the following model: public class Foo { public int Id { get; set;}
public class Foo { public int X { get; set; } public int Y
I have a model public class Foo{ public int Id{get;set;} public string Name {get;

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.