You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@isis.apache.org by Marianne Hagaseth <Ma...@marintek.sintef.no> on 2016/04/19 20:37:13 UTC

Comparing numbers as string

Hi,
Which function to use to be able to return "1","2","3","10","11", and not "1","10","11","2","3"  (String values)?

public static class RegulationsComparator implements Comparator<Regulation> {
    @Override
    public int compare(Regulation p, Regulation q) {
        Ordering<Regulation> byRegulationNumber = new Ordering<Regulation>() {
          public int compare(final Regulation p, final Regulation q) {
            return Ordering.natural().nullsFirst().compare(p.getRegulationNumber(),q.getRegulationNumber());
            }
        };
        return byRegulationNumber
                .compound(Ordering.<Regulation>natural())
                .compare(p, q);
    }
}



Med vennlig hilsen / Best regards,
Marianne Hagaseth
Forsker - Maritime transportsystemer
Research Scientist - Maritime Transport Systems

MARINTEK (Norsk Marinteknisk Forskningsinstitutt AS)
Address:  POB 4125 Valentinlyst, NO-7450 Trondheim, Norway
Mobile:   +47 90 95 64 69 -  Phone: +47 464 15 000
Web:       www.marintek.sintef.no<http://www.marintek.sintef.no/>


RE: Comparing numbers as string

Posted by Marianne Hagaseth <Ma...@marintek.sintef.no>.
Thanks a lot!

Med vennlig hilsen / Best regards,
Marianne Hagaseth 
Forsker – Maritime transportsystemer
Research Scientist – Maritime Transport Systems
 
MARINTEK (Norsk Marinteknisk Forskningsinstitutt AS)
Address:  POB 4125 Valentinlyst, NO-7450 Trondheim, Norway
Mobile:   +47 90 95 64 69 -  Phone: +47 464 15 000
Web:       www.marintek.sintef.no


-----Original Message-----
From: Dan Haywood [mailto:dan@haywood-associates.co.uk] 
Sent: 19. april 2016 21:13
To: users <us...@isis.apache.org>
Subject: Re: Comparing numbers as string

Hi Marianne,

I think you just need to do an Integer.parseInt(...) around each call to p.getRegulationNumber().  I guess if the value is non parseable then it should catch any exception and return a suitable value.

ie, replace:

            p.getRegulationNumber()

with

            parseInt(p.getRegulationNumber())

where

            int parseInt(String str) {
               try{ return Integer.parseInt(str); } catch(Exception ex) { return -1; }
            }


HTH
Dan





On 19 April 2016 at 19:37, Marianne Hagaseth < Marianne.Hagaseth@marintek.sintef.no> wrote:

> Hi,
> Which function to use to be able to return "1","2","3","10","11", and 
> not "1","10","11","2","3"  (String values)?
>
> public static class RegulationsComparator implements 
> Comparator<Regulation> {
>     @Override
>     public int compare(Regulation p, Regulation q) {
>         Ordering<Regulation> byRegulationNumber = new
> Ordering<Regulation>() {
>           public int compare(final Regulation p, final Regulation q) {
>             return
> Ordering.natural().nullsFirst().compare(p.getRegulationNumber(),q.getRegulationNumber());
>             }
>         };
>         return byRegulationNumber
>                 .compound(Ordering.<Regulation>natural())
>                 .compare(p, q);
>     }
> }
>
>
>
> Med vennlig hilsen / Best regards,
> Marianne Hagaseth
> Forsker - Maritime transportsystemer
> Research Scientist - Maritime Transport Systems
>
> MARINTEK (Norsk Marinteknisk Forskningsinstitutt AS)
> Address:  POB 4125 Valentinlyst, NO-7450 Trondheim, Norway
> Mobile:   +47 90 95 64 69 -  Phone: +47 464 15 000
> Web:       www.marintek.sintef.no<http://www.marintek.sintef.no/>
>
>

Re: Comparing numbers as string

Posted by Dan Haywood <da...@haywood-associates.co.uk>.
Hi Marianne,

I think you just need to do an Integer.parseInt(...) around each call to
p.getRegulationNumber().  I guess if the value is non parseable then it
should catch any exception and return a suitable value.

ie, replace:

            p.getRegulationNumber()

with

            parseInt(p.getRegulationNumber())

where

            int parseInt(String str) {
               try{ return Integer.parseInt(str); } catch(Exception ex) {
return -1; }
            }


HTH
Dan





On 19 April 2016 at 19:37, Marianne Hagaseth <
Marianne.Hagaseth@marintek.sintef.no> wrote:

> Hi,
> Which function to use to be able to return "1","2","3","10","11", and not
> "1","10","11","2","3"  (String values)?
>
> public static class RegulationsComparator implements
> Comparator<Regulation> {
>     @Override
>     public int compare(Regulation p, Regulation q) {
>         Ordering<Regulation> byRegulationNumber = new
> Ordering<Regulation>() {
>           public int compare(final Regulation p, final Regulation q) {
>             return
> Ordering.natural().nullsFirst().compare(p.getRegulationNumber(),q.getRegulationNumber());
>             }
>         };
>         return byRegulationNumber
>                 .compound(Ordering.<Regulation>natural())
>                 .compare(p, q);
>     }
> }
>
>
>
> Med vennlig hilsen / Best regards,
> Marianne Hagaseth
> Forsker - Maritime transportsystemer
> Research Scientist - Maritime Transport Systems
>
> MARINTEK (Norsk Marinteknisk Forskningsinstitutt AS)
> Address:  POB 4125 Valentinlyst, NO-7450 Trondheim, Norway
> Mobile:   +47 90 95 64 69 -  Phone: +47 464 15 000
> Web:       www.marintek.sintef.no<http://www.marintek.sintef.no/>
>
>