12,072,067 members (60,705 online)
Rate this:
See more:
I have a staff numbers arranged sequentially
like
1
100
101
102
103
.
.
.
2
200
201
.
.
and i make a binary search on them
but i faced a problem that
in sequential numbers consider 2>103
but when comparing between these two number in binary serach consider 103>2
because the number of digits in 103 bigger than the number of digits in 2
is there a way to compare between sequential number without facing this problem
Posted 24-Mar-13 20:16pm

Rate this:

## Solution 1

No. It makes no sense. 2 ≯ 201, 103 > 2. Your sequence shown in the question is not "sequential", if you mean ordering my its numeric value.

If you need some other order, please explain the ordering comparison rule, or do it by yourself.

—SA
Rate this:

## Solution 2

Don't use strings to store numbers, dates, etc.
Rate this:

## Solution 4

If you have to sort these as a string, and you know the maximum length of the string, then zero-pad them. i.e. if the maximum length is 10 digits, then change "1" to "0000000001". Then the ordering will work.
Howeever, if you know they are always going to consist only of digits, you probably want to store them as integers, as PIEBALDconsult suggested.
Regards,
Ian.
Rate this:

## Solution 3

Staff numbers arranged in the following
1
101
10101
1010101
1010101001
1010102
1010102001
1010102002
1010102003
1010102004
1010102005
1010102006
1010102007
1010102008
1010102009
1010102010
1010102011
1010102012
1010102013
1010102014
1010102015
1010103
1010103001
1010103002
1010103003
1010103004
1010103005
1010104
1010104001
1010104002
1010104003
1010104004
1010104005
1010104006
1010104007
1010104008
1010104009
1010105
1010105001
102
10201
1020101
1020101001
1020102
1020102001
10202
1020201
1020201001
1020201002
1020201003
1020201004
1020201005
10203
1020302
1020302001
10204
1020401
1020401001
1020402
1020402001
1020403
1020403001
1020404
1020404001
1020405
1020405001
1020406
1020406001
1020407
1020407001
1020408
1020408001
10205
1020501
1020501001
1020501002
1020501003
1020501004
1020501005
1020501006
1020501007
1020501008
1020501009
1020501010
1020501011
1020501012
1020501013
1020501014
1020501015
1020501016
1020501017
1020501018
1020501019
1020501020
1020501021
.
.
2
.
.
.
3
.
.
.

this ordering used for ex students numbers in university
ayah abuzaid 28-Mar-13 5:36am

thank you all

Top Experts
Last 24hrsThis month
 OriginalGriff 385 F-ES Sitecore 220 Dave Kreskowiak 215 CPallini 175 Richard MacCutchan 145
 Dave Kreskowiak 2,426 OriginalGriff 1,949 Richard MacCutchan 1,675 CPallini 1,222 CHill60 1,219