getNaturalTextComparison function
Implementation
int getNaturalTextComparison(
String firstText,
String secondText
)
{
var firstCharacterCount = firstText.length;
var secondCharacterCount = secondText.length;
var firstCharacterIndex = 0;
var secondCharacterIndex = 0;
while ( firstCharacterIndex < firstCharacterCount
&& secondCharacterIndex < secondCharacterCount )
{
var firstCharacter = firstText[ firstCharacterIndex ];
var secondCharacter = secondText[ secondCharacterIndex ];
if ( firstCharacter == secondCharacter )
{
++firstCharacterIndex;
++secondCharacterIndex;
}
else
{
var firstCodeUnit = firstCharacter.codeUnitAt( 0 );
var secondCodeUnit = secondCharacter.codeUnitAt( 0 );
if ( firstCodeUnit >= 48
&& firstCodeUnit <= 57
&& secondCodeUnit >= 48
&& secondCodeUnit <= 57 )
{
var firstNumberText = '';
var secondNumberText = '';
while ( firstCharacterIndex < firstCharacterCount
&& firstText[ firstCharacterIndex ].codeUnitAt( 0 ) >= 48
&& firstText[ firstCharacterIndex ].codeUnitAt( 0 ) <= 57 )
{
firstNumberText += firstText[ firstCharacterIndex ];
++firstCharacterIndex;
}
while ( secondCharacterIndex < secondCharacterCount
&& secondText[ secondCharacterIndex ].codeUnitAt( 0 ) >= 48
&& secondText[ secondCharacterIndex ].codeUnitAt( 0 ) <= 57 )
{
secondNumberText += secondText[ secondCharacterIndex ];
++secondCharacterIndex;
}
return int.parse( firstNumberText ) - int.parse( secondNumberText );
}
else
{
return firstCharacter.compareTo( secondCharacter );
}
}
}
return firstCharacterCount - secondCharacterCount;
}