Design Option Sorting Revit 2021

Design Option Sorting Revit 2021

BobbyC.Jones
Advocate Advocate
1,105 Views
13 Replies
Message 1 of 14

Design Option Sorting Revit 2021

BobbyC.Jones
Advocate
Advocate

Can someone please share with me how the Design Options are sorted in Revit 2021.  I'm trying to create an IComparer implementation that will match it.  My attempts have centered around string.Compare() with various flags and options using the docs for CompareStringEx as a guide.  But so far, no luck.  Here is an example:

    public class DesignOptionComparer : IComparer<DesignOption>
    {
        public int Compare(DesignOption x, DesignOption y)
        {
            var result = string.Compare(x.Name, y.Name, CultureInfo.CurrentCulture, CompareOptions.IgnoreSymbols);
            return result;
        }
    }

 

Given two Design Options in the same Option Set, the Revit UI sorts them this way:

 

  • OPT. COVERED PORCH W/ EXT. RETREAT A/B
  • OPT. COVERED PORCH - A/B

Whereas all my Comparers to date return this order

 

  • OPT. COVERED PORCH - A/B
  • OPT. COVERED PORCH W/ EXT. RETREAT A/B

Thanks!

--
Bobby C. Jones
0 Likes
Accepted solutions (1)
1,106 Views
13 Replies
Replies (13)
Message 2 of 14

jeremy_tammik
Alumni
Alumni

I have not analysed the Revit sorting in depth.

 

From what you say, though, it sounds as if you will have to implement some custom sort order to achieve it, e.g., explicitly sort the hyphen character '-' before the alphabetical ones.

 

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 3 of 14

BobbyC.Jones
Advocate
Advocate

Thanks Jeremy.  I can certainly implement custom sorting for the hyphen.  I would love to know if other characters need special handling as well.  There are hints in the Microsoft docs that the apostrophe could be another to look for.  Alphanumeric sorting turns out to be a much more complicated topic than I ever imagined. 

--
Bobby C. Jones
0 Likes
Message 4 of 14

RPTHOMAS108
Mentor
Mentor

If you remove all non alphanumerical characters from the strings before you sort them then such characters are of no consequence unless you have two strings distinguished only by those characters.

 

Similar to when using String.ToLower to ignore case in comparisons. That is probably the behaviour you actually want since hyphen is always going to mess up the order. It isn't so much a case of which punctuation comes before which since nobody could really tell you if . should be before - or _ (those are probably sorted by character code).

 

If you are working with ASCII characters only then you can exclude by code:

ASCII

0-9 = 48-57 

A-Z = 65-90

_ = 95

a-z = 97-122

 

For Unicode you probably have to use Char.IsNumber & Char.IsLetter etc. then probably culture comes into play in terms of how people expect things in their culture to be sorted.

 

 

0 Likes
Message 5 of 14

BobbyC.Jones
Advocate
Advocate

That's an interesting thought.  I'll test to see if that matches how the Revit UI sorts them.

--
Bobby C. Jones
0 Likes
Message 6 of 14

RPTHOMAS108
Mentor
Mentor

I don't understand how Revit is sorting those:

 

OPT. COVERED PORCH W/ EXT. RETREAT A/B
OPT. COVERED PORCH - A/B

 

For the above counting from left to right (after removing -) you would still expect A/B... to come before W/EXT... probably the extra space after the hyphen is dictating order. The way your comparer has it makes more sense to me than the Revit version (as a human being I expect 'A' before 'W').

0 Likes
Message 7 of 14

jeremy_tammik
Alumni
Alumni

Maybe it would help to do the following:

  

  • Ensure all your names consist purely of ASCII characters and nothing else
  • Programmatically create 128 elements named with a single character from ASCII 0 to 127
  • Observe the sorting order created by Revit

 

That would at least provide a reliable starting point...

 

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 8 of 14

FAIR59
Advisor
Advisor
Accepted solution

In the NamingUtils , I find the method  CompareNames(), 

Return ValueAn integer indicating the result of the lexical comparison between the two names. Less than zero if nameA comes before nameB in the ordering, zero if nameA and nameB are equivalent, and greater than zero if nameA is comes after nameB in the ordering.

Remarks

This routine is similar to System.String.Compare(), but uses Revit rules for comparison. This involves breaking the names into alphabetic and numeric tokens and comparing tokens individually. Neither comparand is allowed to be a null reference ( Nothing in Visual Basic) .
Message 9 of 14

BobbyC.Jones
Advocate
Advocate

@FAIR59 You are my hero, again!  This works perfectly.

--
Bobby C. Jones
0 Likes
Message 10 of 14

BobbyC.Jones
Advocate
Advocate

@RPTHOMAS108 wrote:

I don't understand how Revit is sorting those:

 

OPT. COVERED PORCH W/ EXT. RETREAT A/B
OPT. COVERED PORCH - A/B

 

For the above counting from left to right (after removing -) you would still expect A/B... to come before W/EXT... probably the extra space after the hyphen is dictating order. The way your comparer has it makes more sense to me than the Revit version (as a human being I expect 'A' before 'W').


I stopped short of extensive sort testing, thanks to @FAIR59 , but I did find that this character isn't a hyphen, it is an 'en dash'.  I believe a true hyphen sorts prior to the W.

--
Bobby C. Jones
0 Likes
Message 11 of 14

RPTHOMAS108
Mentor
Mentor

Yes I don't know enough about what they mean in terms of 'Revit rules for comparison' what are those exactly?

 

Is it doing it similar to current project browser settings e.g.:

Browser Organization Properties Dialog: Grouping and Sorting Tab | Revit Products | Autodesk Knowled...

"Using:
Indicate whether the items are to be grouped based on all characters of the parameter values, or one or more leading characters.
For example, suppose sheet names use prefixes of AB-, AC-, BD-, BE-, and so. To group together all sheets based on the first letter of their name (A, B, and so on), for Using, select Leading characters, and set it equal to 1."

 

No this if for grouping which makes sense actually but still the term 'Revit rules' (when those rule are not set out) troubles me. What was wrong with string.compare is what I wonder? Perhaps it is the case sensitivity aspect but I've not checked that.

0 Likes
Message 12 of 14

BobbyC.Jones
Advocate
Advocate
Honestly, I prefer the API from Autodesk solution over finding the proper string.compare settings. That way they can black box the rules and we just don't care what they are.
--
Bobby C. Jones
0 Likes
Message 13 of 14

RPTHOMAS108
Mentor
Mentor

Well to be honest as a Revit user I kind of assume an alphabetical order was the right way to go generally. On the other hand sometimes you find things listed by level elevation e.g. levels. I think the idea being that if you are on Level 1m then Slab Level 1m will be nearby and not after Level 100m. That makes sense.

 

Not sure if things like this is what they mean because they haven't clarified it.

 

 

0 Likes
Message 14 of 14

BobbyC.Jones
Advocate
Advocate
Oh, couldn't agree more that order is very important to the user. The reason I posted this in the first place was that Revit 2021 sorting broke some features of our custom Design Option UI, which allows custom sorting.
--
Bobby C. Jones
0 Likes