按多个数字排序Java String数组

Loo*_*The 5 java arrays string

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

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
Run Code Online (Sandbox Code Playgroud)

我的任务是按照每个标准对这些数据进行排序,例如按日期,纬度和经度排序

我尝试过像这样的冒泡

if ( Double.parseDouble(a[0].split(",")[1]) <  Double.parseDouble(a[1].split(",")[1]))
Run Code Online (Sandbox Code Playgroud)

这工作但需要太多时间

40000txt文件中的theres 数据

有没有其他方法来排序这些数据?

Bas*_*que 0

我可能毁了一些学生的作业\xe2\x80\x99,但是这里是\xe2\x80\xa6

\n\n

正如问题中所建议的,Java 中的自然方法是创建一个类来表示您的数据。然后实现一个Comparator要传递给实用程序的方法Collections.sort

\n\n

在运行 Java 8 的 Parallels 虚拟机的 MacBook Pro 2.3 GHz Intel Core i7 上,对包含 42,000 个元素的数据集进行排序需要 45-90 毫秒。

\n\n

我将您的示例数据更改为更有趣,引入了一些不同的日期和重复的纬度。

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

我的GeoReading类来表示数据。

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

这是比较器的实现。

\n\n
class GeoReadingAscendingComparator implements Comparator<GeoReading>\n{\n\n    @Override\n    public int compare( GeoReading o1 , GeoReading o2 )\n    {\n        int localDateCompare = o1.localDate.compareTo( o2.localDate );\n        if ( localDateCompare != 0 ) { // If not equal on this component, so compare on this.\n            return localDateCompare;\n        }\n\n        int latitudeCompare = o1.latitude.compareTo( o2.latitude );\n        if ( latitudeCompare != 0 ) { // If not equal on this component, so compare on this.\n            return latitudeCompare;\n        }\n\n        return o1.longitude.compareTo( o2.longitude );\n\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

主要代码。

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

在现实世界中,我会添加一些防御性编程代码来验证传入的数据。来自外部来源的数据总是有缺陷和/或变化的。

\n