按多个数字对Java字符串数组排序

按多个数字对Java字符串数组排序,java,arrays,string,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,-

我有一个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
我的任务是按每个标准对这些数据进行排序,例如按日期、纬度和经度排序

我试过像这样的泡泡

if ( Double.parseDouble(a[0].split(",")[1]) <  Double.parseDouble(a[1].split(",")[1]))
if(Double.parseDouble(a[0].split(“,”[1])
这是可行的,但需要太多时间

txt文件中有
40000
数据


是否有其他方法对这些数据进行排序

试试看。合并排序的最坏情况性能为O(n logn)。泡泡排序的最坏情况是O(n^2)。

我可能会破坏一些学生的家庭作业,但接下来

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

在我的MacBook Pro 2.3 GHz Intel Core i7上,运行带有Java 8的Parallels虚拟机,一个包含42000个元素的数据集排序需要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
My
GeoReading
类来表示数据

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 + '}';
    }

}
类地理阅读
{
LocalDate LocalDate=null;
BigDecimal纬度=空;
BigDecimal经度=空;
BigDecimal深度=空;
BigDecimal幅值=零;
公共地理阅读(字符串arg)
{
//字符串是以逗号分隔的值:日期、纬度、经度、深度、磁力
List items=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.magnity=新的BigDecimal(items.get(4));
}
@凌驾
公共字符串toString()
{
返回“georreading{”+“localDate=“+localDate+”,latitude=“+latitude+”,longitude=“+longitude+”,depth=“+depth+”,magnistent=“+magnistent+”}”;
}
}
下面是比较器实现

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 );

    }
}
类GeoReadingAscendingComparator实现Comparator
{
@凌驾
公共整数比较(地理读数o1,地理读数o2)
{
int localDateCompare=o1.localDate.compareTo(o2.localDate);
如果(localDateCompare!=0){//如果此组件上的值不相等,则在此组件上进行比较。
返回localDateCompare;
}
int-latitudeCompare=o1.纬度比(o2.纬度);
如果(latitudeCompare!=0){//如果此组件上的值不相等,则在此组件上进行比较。
返回纬度比较;
}
返回o1.经度.compareTo(o2.经度);
}
}
主代码

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 );
}
Path Path=Path.get(“/Users/basil/lat lon.txt”);//MacOSX的路径。
试一试{
列表=新的ArrayList();
流行=文件。行(路径);
行.forEach(行->列表.add(新的地理位置(行));
//将这14行相乘以模拟大型文本文件。14*3000=42000。
整数计数=3000;
List bigList=new ArrayList(List.size()*count);//将电容初始化为预期的元素数。
for(int i=0;i

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

将每一行存储在一个类对象中,然后为该类对象的列表定义不同的比较器。将此用作参考文件合并排序如何?O(nlogn)如果你不知道这个符号是什么意思,它就是大的Oh符号。O(nlogn)比O(n^2)好,这意味着,在最坏的情况下,合并排序将比冒泡排序执行得更好。更多信息请参见此处:(这将对您的未来非常重要)。为什么选择BigXXX字段?为什么不加倍和整数?@user949300精度>性能。我有一个习惯,在我通常的项目中使用金钱,在需要精确性的地方。如果这些科学数据能够容忍浮点漂移的不精确性,那就去做吧。可能会更快,但是对于总执行时间不到一秒钟,我会考虑。OP有40K记录,每个字段有5个,所以使用BigXXX是200K对象。一个对象大约比一个原语多10个字节,所以是2MB。我想,这在今天已经不算什么了,但它确实是一个东西。@user949300bigdecimal不仅仅是您提到的最小对象开销。但在多gig机器上仍然没有大量内存。如果内存是一个问题,我会将数据移动到一个类似的数据库中。因此,这不是偏好的问题,而是需要。如果您确定浮点i