You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于基础逻辑实现DD/MM/YYYY格式出生日期的记录排序

Got it, I see exactly where you're stuck here. Using String.compareTo() directly on DD/MM/YYYY dates doesn't work because string comparison checks characters left to right—so it'll compare days first, then months, then years, which is the opposite of the chronological order we need. Let's fix this with basic logic since you can't use collections like ArrayList.

Core Idea: Parse Date Components for Comparison

Instead of comparing the date strings directly, we'll split each DD/MM/YYYY string into its day, month, and year parts, convert them to integers, then compare in the correct priority: year first, then month, then day. This gives us the proper chronological order.

Step-by-Step Implementation

  1. Split Date Strings: Use split("/") to break each date into three parts, then parse them into integers.
  2. Adjust Comparison Logic: Replace the string comparison with a custom check that prioritizes year → month → day.
  3. Sync All Array Swaps: Since your data is spread across four arrays (first name, last name, DOB, percentage), we need to swap elements in all arrays at the same time to keep records intact.

Modified Full Code

import java.util.Scanner;

class Sortbydob {
    // Sort records by date of birth (DD/MM/YYYY) in ascending order
    void sortString(String[] First_name, String[] Last_name, String[] Dob, int[] Percent) {
        String tempValue;
        int tempHold;
        
        // Basic bubble sort (perfect for small datasets like 3 records)
        for (int i = 0; i < Dob.length; i++) {
            for (int j = i + 1; j < Dob.length; j++) {
                // Check if Dob[i] is chronologically later than Dob[j]
                if (isDateLater(Dob[i], Dob[j])) {
                    // Swap first names
                    tempValue = First_name[i];
                    First_name[i] = First_name[j];
                    First_name[j] = tempValue;
                    
                    // Swap last names
                    tempValue = Last_name[i];
                    Last_name[i] = Last_name[j];
                    Last_name[j] = tempValue;
                    
                    // Swap DOBs
                    tempValue = Dob[i];
                    Dob[i] = Dob[j];
                    Dob[j] = tempValue;
                    
                    // Swap percentages
                    tempHold = Percent[i];
                    Percent[i] = Percent[j];
                    Percent[j] = tempHold;
                }
            }
        }
        
        // Optional: Print sorted records to verify
        System.out.println("\nSorted Records:");
        for (int i = 0; i < Dob.length; i++) {
            System.out.printf("%s %s | %s | %d%%\n", First_name[i], Last_name[i], Dob[i], Percent[i]);
        }
    }
    
    // Helper method: Returns true if date1 is later than date2
    private boolean isDateLater(String date1, String date2) {
        // Split date1 into day, month, year
        String[] parts1 = date1.split("/");
        int day1 = Integer.parseInt(parts1[0]);
        int month1 = Integer.parseInt(parts1[1]);
        int year1 = Integer.parseInt(parts1[2]);
        
        // Split date2 into day, month, year
        String[] parts2 = date2.split("/");
        int day2 = Integer.parseInt(parts2[0]);
        int month2 = Integer.parseInt(parts2[1]);
        int year2 = Integer.parseInt(parts2[2]);
        
        // Compare years first
        if (year1 > year2) {
            return true;
        } else if (year1 == year2) {
            // Same year? Compare months
            if (month1 > month2) {
                return true;
            } else if (month1 == month2) {
                // Same month? Compare days
                return day1 > day2;
            }
        }
        // date1 is earlier or equal, no swap needed
        return false;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Sortbydob obj = new Sortbydob(); // Fixed class name mismatch
        int recordCount = 3;
        String[] First_name = new String[recordCount];
        String[] Last_name = new String[recordCount];
        String[] Dob = new String[recordCount];
        int[] Percent = new int[recordCount];
        
        for (int i = 0; i < recordCount; i++) {
            System.out.println("Enter the First Name : ");
            First_name[i] = in.next();
            System.out.println("Enter the Last Name : "); // Fixed input prompt typo
            Last_name[i] = in.next();
            System.out.println("Enter the Dob in DD/MM/YYYY Format : ");
            Dob[i] = in.next();
            System.out.println("Enter the Percentage : ");
            Percent[i] = in.nextInt();
        }
        
        // Trigger sorting
        obj.sortString(First_name, Last_name, Dob, Percent);
        in.close();
    }
}

Key Notes

  • Helper Method isDateLater: This encapsulates the date comparison logic, making the code cleaner and easier to adjust later.
  • Bubble Sort Fit: Since you're only working with 3 records, bubble sort is simple and efficient enough for this use case.
  • Synced Swaps: All four arrays are swapped together to ensure each person's name, DOB, and percentage stay linked correctly.
  • Fixed Minor Bugs: Corrected the class name mismatch and input prompt typo from your original code to ensure it runs smoothly.

内容的提问来源于stack exchange,提问作者user12146688

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:15:53