Giter Club home page Giter Club logo

numberstringcomparer's Introduction

NumberStringComparer

A C# Comparer to sort lists of data that could be either numeric of string replacing the default alphabetical sort which doesn't always work for numbers.

The Problem

The default alphabetical sort doesn't work when numbers are stored as strings.
Example 1:

var list1 = new List<string>(){
    "1",
    "2",
    "3",
    "10",
    "11",
    "100",
};
list1.Sort();

// Result: string.Join(Environment.NewLine, list1);

"1"
"10"
"100"
"11"
"2"
"3"

Example 2:

var list3 = new List<object>(){
    "1",
    "2",
    "3",
    "10",
    "11",
    "100",
    "A",
    "a",
    "b",
    "ab",
};
list3.Sort();

// Result: 
"1"
"10"
"100"
"11"
"2"
"3"
"A"
"a"
"b"
"ab"

Example 3: Combined lists containing both numbers and strings because you can't parse every item as numbers and it fails to compare items of different types.

var list3 = new List<object>(){
    1,
    2,
    3,
    10,
    11,
    100,
    "A",
    "a",
    "b",
    "ab",
};
list3.Sort();
Result:
- InvalidOperationException: 'Failed to compare two elements in the array.'

Solution

NumberStringComparer<T> parses each item into a NumberString<T> object composed of a text property containing the original text and if the item is numeric also a number component. If an item is not completely numeric, number will be null and the text is parsed into a list of parts containing the text broken up into its numeric and non-numeric parts.
e.g. "a122bc4" will result in parts = ["a", 122, "bc", 4]
Items are compared by number if they are completely numeric, otherwise each parts list will be compared for sorting. If the text contains commas, is broken up into a parts array of numbers, removing the commas. e.g. "1,2,3" will result in [1, 2, 3] and "1,2" will result in [1, 2] and when sorting, "1,2" will come before "1,2,3"

Comparing complex objects by 1 property

You can use any of the following primitive collection types

Supported data types

  • string
  • int
  • double
  • float
  • decimal
  • long
  • short

As well as complex objects or Dictionaries using a property. For Dictionaries, it will use the key property of KeyValuePair and ignore the Value property. See NumberStringComparerTests.NumberStringComparerObjectComparison_Tests() for more complex object comparison examples.

.NET Version

This project uses .NET 7.0 (Core) for the main branch but there is also a .NET Framework-compatible branch made with only a few minor changes.

  • Replacing readonly properties with private fields and properties with private do nothing setters.
  • Replace uses of the Range operator with the SubStringSafe extension methods.
  • Explicitly specify the object type on the right hand of equals replacing new()

numberstringcomparer's People

Contributors

sleekpanther avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.