Sorting by Object Number

It is often desireable to sort objects according to object number as this reflects the ordering of objects within a module.

if ("1.10" > "1.5")
{
    print("We want to see this")
}

We can however pad out a legal number with leading zeroes using the lexNum() function so that it can be used lexicographically in comparisons or for sorting. Now running the following does produce the required output:

#include <lexnum.inc>

if (lexNum("1.10") > lexNum("1.5"))
{
    print("We want to see this")
}

Source code:

//  lexnum - convert legal number

/*
	lexnum - pad out object number with zeros so it can be used
	for comparisons or for sorting.

	Usage: string lexNum(Object o|string s)

	smartDXL.com

	Acknowledgement:
	This script was originally taken from the Telelogic kitchen
	and has been modified for use here.
*/

/*************************************
  lexNum

  returns the object number padded out with leading zeroes
  so that it can be used lexicographically in comparisons or for sorting.

  Ensure that s is a valid legal number if using this version.
*************************************/
string lexNum(string s)
{
    // description of first part of legal number
    Regexp firstNum = regexp "^([0-9]+)([.-]|$)"

    Buffer zeros  = create
    Buffer numA   = create
    Buffer aN     = create
    Buffer result = create
    string retVal

    zeros  = "000000"
	numA   = s
    result = ""

    // loop through legal number
    while (firstNum numA)
    {
        aN = numA[match 1]
        result += "." tempStringOf(zeros[1:5-length(aN)]) tempStringOf(aN)
        numA = numA[(end 0)+1:]
    }

    retVal = stringOf(result)

    delete(zeros)
    delete(numA)
    delete(aN)
    delete(result)

    return retVal[1:]
}

/*************************************
  lexNum

  returns the object number padded out with leading zeroes
  so that it can be used lexicographically in comparisons or for sorting.
*************************************/
string lexNum(Object o)
{
    return(lexNum(number(o)))
}