You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Luis Gomez <lg...@gmail.com> on 2010/03/17 12:05:06 UTC

Sorting padded numbers?

Hi, quick one this time...

How can I make a map function sort these keys numerically? Is there an
easy/conventional way or do I need to pad the numbers?

1
10
11
12
13
14
15
16
17
18
19
2
20
21
22
23
24
25
26
27
28
29
3
30
31
32
33
34
35
36
37
38
39
...

Thanks,

Luis

Re: Sorting padded numbers?

Posted by David Goodlad <da...@goodlad.ca>.
On Wed, Mar 17, 2010 at 10:05 PM, Luis Gomez <lg...@gmail.com> wrote:
> Hi, quick one this time...
>
> How can I make a map function sort these keys numerically? Is there an
> easy/conventional way or do I need to pad the numbers?

Hi Luis

Emitting integer keys instead of strings will cause it to sort properly:

function(doc) { emit(parseInt(doc.yourval), "foo" }

Dave

Re: Sorting padded numbers?

Posted by Luis Gomez <lg...@gmail.com>.
Ah... awesome. Simpler than I thought.

ParseInt did the trick but I will store these as numbers instead. That's the
right thing to do in this case.

Thanks!

Luis


On Wed, Mar 17, 2010 at 4:26 AM, Jon Gretar Borgthorsson <
jon.borgthorsson@gmail.com> wrote:

> If you get sorting like that it means that you have strings but not
> numbers.
> Either start using numbers for the values or if that is not possible
> convert
> the string to an integer in the emit function.
>
>   emit(parseInt(doc.my_number), doc);
>
> -- Jón Grétar Borgþórsson
>
>
> On Wed, Mar 17, 2010 at 11:05 AM, Luis Gomez <lg...@gmail.com> wrote:
>
> > Hi, quick one this time...
> >
> > How can I make a map function sort these keys numerically? Is there an
> > easy/conventional way or do I need to pad the numbers?
> >
> >
> > Thanks,
> >
> > Luis
> >
>

Re: Sorting padded numbers?

Posted by Jon Gretar Borgthorsson <jo...@gmail.com>.
If you get sorting like that it means that you have strings but not numbers.
Either start using numbers for the values or if that is not possible convert
the string to an integer in the emit function.

   emit(parseInt(doc.my_number), doc);

-- Jón Grétar Borgþórsson


On Wed, Mar 17, 2010 at 11:05 AM, Luis Gomez <lg...@gmail.com> wrote:

> Hi, quick one this time...
>
> How can I make a map function sort these keys numerically? Is there an
> easy/conventional way or do I need to pad the numbers?
>
>
> Thanks,
>
> Luis
>

Re: Sorting padded numbers?

Posted by Patrick Barnes <mr...@gmail.com>.
Luis - if you emit them as numbers rather than strings, they will sort in
the proper order.

>From memory, I think one of the string methods is used to convert string to
int.

Alternatively, consider storing those properties as numbers in the first
place - not only will your keys sort properly, but it will probably use less
space per doc and build views faster.

HTH,
-Patrick

On 17/03/2010 10:05 PM, "Luis Gomez" <lg...@gmail.com> wrote:

Hi, quick one this time...

How can I make a map function sort these keys numerically? Is there an
easy/conventional way or do I need to pad the numbers?

1
10
11
12
13
14
15
16
17
18
19
2
20
21
22
23
24
25
26
27
28
29
3
30
31
32
33
34
35
36
37
38
39
...

Thanks,

Luis