Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB databse
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
please help
Posted 24-Mar-13 20:16pm
Rate this: bad
good
Please Sign up or sign in to vote.

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Don't use strings to store numbers, dates, etc.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

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
  Permalink  
Comments
ayah abuzaid at 28-Mar-13 5:36am
   
thank you all
I found your answer useful

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 475
1 Maciej Los 349
2 Richard MacCutchan 220
3 BillWoodruff 219
4 Mathew Soji 160
0 OriginalGriff 8,759
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,639
3 Maciej Los 5,269
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web04 | 2.8.1411028.1 | Last Updated 25 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100