排序的Java String数组由多个号码 [英] Sort Java String array by multiple numbers

查看:128
本文介绍了排序的Java String数组由多个号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有数据的列表,在这样一个txt文件

 日期,纬度,经度,深度,马格20000101,34.6920,-116.3550,12.30,1.21
20000101,34.4420,-116.2280,7.32,1.01
20000101,37.4172,-121.7667,5.88,1.14
20000101,-41.1300,174.7600,27.00,1.90
20000101,37.6392,-119.0482,2.40,1.03
20000101,32.1790,-115.0730,6.00,2.44
20000101,59.7753,-152.2192,86.34,1.48
20000101,34.5230,-116.2410,11.63,1.61
20000101,59.5369,-153.1360,100.15,1.62
20000101,44.7357,-110.7932,4.96,2.20
20000101,34.6320,-116.2950,​​9.00,1.73
20000101,44.7370,-110.7938,5.32,1.75
20000101,35.7040,-117.6320,4.15,1.45
20000101,41.9270,20.5430,10.00,4.80

我的任务是由每个标准前这些数据),按日期排序,纬度和经度

排序

我试图冒泡排序是这样

 如果(Double.parseDouble(A [0] .split()[1])LT; D​​ouble.parseDouble(A [1] .split() [1]))

这工作,但需要太多的时间

孤单 40000 的txt文件数据

有没有对这些数据进行排序的另一种方式?


解决方案

我可能破坏了一些学生的家庭作业,但在这里不用...

关于这一问题的建议,在Java中自然的方式是创建一个类来重新present您的数据。然后实现一个 比较 传递给实用方法 Col​​lections.sort

在我的MacBook Pro 2.3 GHz的英特尔酷睿i7运行的Parallels虚拟机与Java 8 42,000元素的一组数据需要45-90毫秒之间进行排序。

我改变你的榜样的数据会更有趣,引入一些不同的日期和重复的纬度。

20000101,34.6920,-116.3550,12.30,1.21
20000101,34.4420,-116.2280,7.32,1.01
20000101,34.6920,-121.7667,5.88,1.14
20000101,-41.1300,174.7600,27.00,1.90
20000101,37.6392,-119.0482,2.40,1.03
20000101,32.1790,-115.0730,6.00,2.44
20000101,34.6920,-152.2192,86.34,1.48
20000102,34.6320,-116.2410,11.63,1.61
20000102,59.5369,-153.1360,100.15,1.62
20000102,44.7357,-110.7932,4.96,2.20
20000102,34.6320,-116.2950,​​9.00,1.73
20000102,34.6320,-110.7938,5.32,1.75
20000102,34.6320,-117.6320,4.15,1.45
20000102,41.9270,20.5430,10.00,4.80

我的 GeoReading 类重新present的数据。

 类GeoReading
{    LOCALDATE LOCALDATE = NULL;
    BigDecimal的纬度= NULL;
    BigDecimal的经度= NULL;
    BigDecimal的深度= NULL;
    BigDecimal的幅度= NULL;    公共GeoReading(字符串ARG)
    {
        // string是逗号分隔值:日期,纬度,经度,深度,马格
        清单<串GT;项目= Arrays.asList(arg.split(\\\\ S *,\\\\ S *)); //正则表达式了说明:http://stackoverflow.com/a/7488676/642706
        this.localDate = ISODateTimeFormat.basicDate()parseLocalDate(items.get(0))。
        this.latitude =新的BigDecimal(items.get(1));
        this.longitude =新的BigDecimal(items.get(2));
        this.depth =新的BigDecimal(items.get(3));
        this.magnitude =新的BigDecimal(items.get(4));
    }    @覆盖
    公共字符串的toString()
    {
        回归GeoReading {+= LOCALDATE+ + LOCALDATE,纬度=+纬度+,东经=+经度+,深​​度=+深度+级=+级+'};
    }}

下面是比较落实。

 类GeoReadingAscendingComparator实现比较< GeoReading>
{    @覆盖
    公众诠释比较(GeoReading O1,O2 GeoReading)
    {
        INT localDateCompare = o1.localDate.compareTo(o2.localDate);
        如果(localDateCompare!= 0){//如果不相等此组件上,所以比较于此。
            返回localDateCompare;
        }        INT latitudeCompare = o1.latitude.compareTo(o2.latitude);
        如果(latitudeCompare!= 0){//如果不相等此组件上,所以这个比较。
            返回latitudeCompare;
        }        返回o1.longitude.compareTo(o2.longitude);    }
}

主要code。

 路径path = Paths.get(/Users/basil/lat-lon.txt); //路径的Mac OS X.
尝试{
    清单< GeoReading>名单=新的ArrayList<>();
    流<串GT;线= Files.lines(路径);
    lines.forEach(线 - > list.add(新GeoReading(线)));
    //把这些14行和繁殖,以模拟大的文本文件。 14 * 3000 = 42,000
    诠释计数= 3000;
    清单< GeoReading> bigList =新的ArrayList<>(则为list.size()*计); // Initialze capacite组成元素预期数量。
    的for(int i = 0; I<计数;我++){
        bigList.addAll(名单);
    }
    长启动= System.nanoTime();
    Collections.sort(bigList,新GeoReadingAscendingComparator());
    经过长=(System.nanoTime() - 启动);
    的System.out.println(完成排序GeoReading列表排序+ bigList.size()+了:+ TimeUnit.MILLISECONDS.convert(已过,TimeUnit.NANOSECONDS)+毫秒(+经过+毫微秒) 。);    的System.out.println(转储...);
    对于(GeoReading G:bigList){
        的System.out.println(G);
    }
}赶上(IOException异常前){
    的System.out.println(错误 - 比如:恩+);
}

在现实世界中我会添加一些防御性的编程code,以验证输入的数据。从外部来源数据的总是的缺陷和/或改变。<​​/ P>

I have a list of data in a txt file like this

Date,Lat,Lon,Depth,Mag

20000101,34.6920,-116.3550,12.30,1.21
20000101,34.4420,-116.2280,7.32,1.01
20000101,37.4172,-121.7667,5.88,1.14
20000101,-41.1300,174.7600,27.00,1.90
20000101,37.6392,-119.0482,2.40,1.03
20000101,32.1790,-115.0730,6.00,2.44
20000101,59.7753,-152.2192,86.34,1.48
20000101,34.5230,-116.2410,11.63,1.61
20000101,59.5369,-153.1360,100.15,1.62
20000101,44.7357,-110.7932,4.96,2.20
20000101,34.6320,-116.2950,9.00,1.73
20000101,44.7370,-110.7938,5.32,1.75
20000101,35.7040,-117.6320,4.15,1.45
20000101,41.9270,20.5430,10.00,4.80

my assignment is to sort these data by each criterion ex) sort by date, latitude and longtitude

i tried bubble sort like this

if ( Double.parseDouble(a[0].split(",")[1]) <  Double.parseDouble(a[1].split(",")[1]))

this works but takes too much time

theres 40000 data in the txt file

is there any alternative way to sort these data?

解决方案

I'm probably ruining some students’ homework assignment, but here goes…

As suggested on the Question, the natural way in Java is to create a class to represent your data. Then implement a Comparator to be passed to the utility method Collections.sort.

On my MacBook Pro 2.3 GHz Intel Core i7 running a Parallels virtual machine with Java 8, a data set of 42,000 elements takes between 45-90 milliseconds to sort.

I changed your example data to be more interesting, introducing some varied dates and duplicate latitudes.

20000101,34.6920,-116.3550,12.30,1.21
20000101,34.4420,-116.2280,7.32,1.01
20000101,34.6920,-121.7667,5.88,1.14
20000101,-41.1300,174.7600,27.00,1.90
20000101,37.6392,-119.0482,2.40,1.03
20000101,32.1790,-115.0730,6.00,2.44
20000101,34.6920,-152.2192,86.34,1.48
20000102,34.6320,-116.2410,11.63,1.61
20000102,59.5369,-153.1360,100.15,1.62
20000102,44.7357,-110.7932,4.96,2.20
20000102,34.6320,-116.2950,9.00,1.73
20000102,34.6320,-110.7938,5.32,1.75
20000102,34.6320,-117.6320,4.15,1.45
20000102,41.9270,20.5430,10.00,4.80

My GeoReading class to represent the data.

class GeoReading
{

    LocalDate localDate = null;
    BigDecimal latitude = null;
    BigDecimal longitude = null;
    BigDecimal depth = null;
    BigDecimal magnitude = null;

    public GeoReading( String arg )
    {
        // String is comma-separated values of: Date,Lat,Lon,Depth,Mag
        List<String> items = Arrays.asList( arg.split( "\\s*,\\s*" ) ); // Regex explained here: http://stackoverflow.com/a/7488676/642706
        this.localDate = ISODateTimeFormat.basicDate().parseLocalDate( items.get( 0 ) );
        this.latitude = new BigDecimal( items.get( 1 ) );
        this.longitude = new BigDecimal( items.get( 2 ) );
        this.depth = new BigDecimal( items.get( 3 ) );
        this.magnitude = new BigDecimal( items.get( 4 ) );
    }

    @Override
    public String toString()
    {
        return "GeoReading{" + "localDate=" + localDate + ", latitude=" + latitude + ", longitude=" + longitude + ", depth=" + depth + ", magnitude=" + magnitude + '}';
    }

}

Here is the Comparator implementation.

class GeoReadingAscendingComparator implements Comparator<GeoReading>
{

    @Override
    public int compare( GeoReading o1 , GeoReading o2 )
    {
        int localDateCompare = o1.localDate.compareTo( o2.localDate );
        if ( localDateCompare != 0 ) { // If not equal on this component, so compare on this.
            return localDateCompare;
        }

        int latitudeCompare = o1.latitude.compareTo( o2.latitude );
        if ( latitudeCompare != 0 ) { // If not equal on this component, so compare on this.
            return latitudeCompare;
        }

        return o1.longitude.compareTo( o2.longitude );

    }
}

Main code.

Path path = Paths.get( "/Users/basil/lat-lon.txt" );  // Path for Mac OS X.
try {
    List<GeoReading> list = new ArrayList<>();
    Stream<String> lines = Files.lines( path );
    lines.forEach( line -> list.add( new GeoReading( line ) ) );
    // Take those 14 lines and multiply to simulate large text file. 14 * 3,000 = 42,000.
    int count = 3000;
    List<GeoReading> bigList = new ArrayList<>( list.size() * count ); // Initialze capacite to expected number of elements.
    for ( int i = 0 ; i < count ; i++ ) {
        bigList.addAll( list );
    }
    long start = System.nanoTime();
    Collections.sort( bigList , new GeoReadingAscendingComparator() );
    long elapsed = ( System.nanoTime() - start );
    System.out.println( "Done sorting the GeoReading list. Sorting " + bigList.size() + " took: " + TimeUnit.MILLISECONDS.convert( elapsed , TimeUnit.NANOSECONDS ) + " ms ( " + elapsed + " nanos )." );

    System.out.println( "Dump…" );
    for ( GeoReading g : bigList ) {
        System.out.println( g );
    }
} catch ( IOException ex ) {
    System.out.println( "ERROR - ex: " + ex );
}

In the real world I would add some defensive-programming code to verify the incoming data. Data from external sources is always defective and/or changing.

这篇关于排序的Java String数组由多个号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆