Task: Largest int from concatenated ints





Java: import java.util.*;

public class IntConcat {
 
    private static Comparator<Integer> sorter = new Comparator<Integer>(){
        @Override
        public int compare(Integer o1, Integer o2){
            String o1s = o1.toString();
            String o2s = o2.toString();
            
            if(o1s.length() == o2s.length()){
                return o2s.compareTo(o1s);
            }

            int mlen = Math.max(o1s.length(), o2s.length());
            while(o1s.length() < mlen * 2) o1s += o1s;
            while(o2s.length() < mlen * 2) o2s += o2s;
            
            return o2s.compareTo(o1s);
        }
    };
    
    public static String join(List<?> things){
        String output = "";
        for(Object obj:things){
            output += obj;
        }
        return output;
    }
    
    public static void main(String[] args){
        List<Integer> ints1 = new ArrayList<Integer>(Arrays.asList(1, 34, 3, 98, 9, 76, 45, 4));
        
        Collections.sort(ints1, sorter);
        System.out.println(join(ints1));
        
        List<Integer> ints2 = new ArrayList<Integer>(Arrays.asList(54, 546, 548, 60));
        
        Collections.sort(ints2, sorter);
        System.out.println(join(ints2));
    }
}






Fortran:       SUBROUTINE SWAP(A,B)	
       INTEGER A,B,T
        T = B
        B = A
        A = T
      END

      SUBROUTINE BIGUP(TEXT,N)	
       CHARACTER*(*) TEXT(*)	
       INTEGER N		
       INTEGER XLAT(N),L(N)	
       INTEGER I,J,M		
       INTEGER TI,TJ		
       INTEGER LI,LJ		
       INTEGER MSG		
       COMMON /IODEV/ MSG	
        DO I = 1,N	
          XLAT(I) = I		
          L(I) = LEN_TRIM(TEXT(I))	
        END DO		
        WRITE (MSG,1) "Supplied",(TEXT(I)(1:L(I)), I = 1,N)	
    1   FORMAT (A12,":",<N>(A,","))	
Crude bubblesort. No attempt at noting the bounds of swaps made.
        DO M = N,1,-1	
          DO I = 2,M		
            J = I - 1		
            TI = XLAT(I)	
            TJ = XLAT(J)	
            LI = L(TI)		
            LJ = L(TJ)		
            IF (LI .EQ. LJ) THEN	
              IF (TEXT(TI).LT.TEXT(TJ)) CALL SWAP(XLAT(I),XLAT(J))	
             ELSE	
              IF (TEXT(TI)(1:LI)//TEXT(TJ)(1:LJ)	
     1        .LT.TEXT(TJ)(1:LJ)//TEXT(TI)(1:LI))	
     2        CALL SWAP(XLAT(I),XLAT(J))	
            END IF			
          END DO		
        END DO	
        WRITE (MSG,2) "Biggest",(TEXT(XLAT(I))(1:L(XLAT(I))),I = N,1,-1)	
    2   FORMAT (A12,":",<N>(A," "))	
      END	

      PROGRAM POKE
      CHARACTER*4 T1(10)	
      CHARACTER*4 T2(4)		
      INTEGER MSG
      COMMON /IODEV/ MSG
      DATA T1(1:8)/"1","34","3","98","9","76","45","4"/
      DATA T2/"54","546","548","60"/
      MSG = 6		
      WRITE (MSG,1)
    1 FORMAT ("Takes a list of integers and concatenates them so as ",
     1 "to produce the biggest possible number.",/,
     2 "The result is shown with spaces between the parts ",
     3 "to show provenance. Ignore them otherwise."/)
      CALL BIGUP(T1,8)

      WRITE (MSG,*)
      CALL BIGUP(T2,4)

      WRITE (MSG,*) "These are supplied in lexicographical order..."
      CALL BIGUP((/"5","54"/),2)

      WRITE (MSG,*) "But this is not necessarily the biggest order."
      CALL BIGUP((/"5","56"/),2)

      WRITE (MSG,*) "And for those who count..."
      DO I = 1,10
        WRITE (T1(I),"(I0)") I	
      END DO			
      CALL BIGUP(T1,10)
      END