Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8595049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:20:38+00:00 2026-06-12T00:20:38+00:00

I have the following data: a b c d f g h i j

  • 0

I have the following data:

    a b c d f g h i j
    a b d e f h i j
    a b c d e f j k l
    a b c d e f g h m

I would like to output it (into Excel for example) as follows:

    a b c d e f g h i j
    a b   d e f   h i j
    a b c d e f       j k l
    a b c d e f g h         m

In excel terms I want to shift cells across so the text matches up in columns.

NOTE: I have used alphabetic ordering for simplicity but in reality there is no such ordering – but I need to maintain the original sequencing.

Updated Example:

    Original Data                                                   
    a   b   c   d   f   g   h   i   j                   
    a   b   d   e   f   h   i   j                       
    a   b   c   d   e   f   j   k   l                   
    a   b   x   d   e   f   g   h   m                   

    Dougs Output                                                    
    a   b   c   d           f   g   h   i   j           
    a   b       d       e   f       h   i   j           
    a   b   c   d       e   f               j   k   l   
    a   b       d   x   e   f   g   h                   m

    My Manual Output (Required) 
    a   b   c       d       f   g   h   i   j           
    a   b           d   e   f       h   i   j           
    a   b   c       d   e   f               j   k   l   
    a   b       x   d   e   f   g   h                   m

Above x occurs at index 2 but d occurs at indices 2 and 3 therefore x should come before d.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T00:20:40+00:00Added an answer on June 12, 2026 at 12:20 am

    I have solved it in Java. There is a custom compare which looks at the max and min indices of each values and sorts on that. Then I printed them out to screen.

    **Note my data is in a HashMap for reasons not explained here but it could have easily been in a simple List.

    **Note excuse my inexperienced coding practices.

    @Doug-Glancy if you are able to do with in VB it would be great!

    ValueComparator.java

    import java.util.Comparator;
    import java.util.Map;
    
    public class ValueComparator implements Comparator<String> {
    
        private Map<String, Integer[]> base;
    
        public ValueComparator(Map<String, Integer[]> m) {
            this.base = m;
        }
    
        public int compare(String so1, String so2) {
    
            // get the max and min indices from each data peice
            Integer[] o1 = base.get(so1);
            Integer[] o2 = base.get(so2);
    
            // compare their min index first
            if (o1[0] < o2[0]) {
                return -1;
            }
            if (o1[0] == o2[0]) { //if they are the same
                if ( o1[1] < o2[1]) { // then look at the max index
                    return -1;
                }
                else {
                    return 1;
                }                   
            }
            else { 
                return 1;
            }
        }
    }
    

    App.java

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.HashMap;
    import java.util.LinkedHashMap;
    import java.util.LinkedList;
    import java.util.List;
    import java.util.Map;
    import localhost.ValueComparator;
    
    
    public class App 
    {
        public static void main( String[] args )
        {
    
            // create a list to store our original data
            ArrayList<HashMap<String, String>> keyValuePairs = new ArrayList<HashMap<String,String>>();
    
            // add the data to the list
            HashMap<String, String> a = new LinkedHashMap<String, String>();
            a.put("a", "1"); a.put("b", "1"); a.put("c", "1"); a.put("d", "1"); a.put("f", "1"); a.put("g", "1"); a.put("h", "1"); a.put("i", "1"); a.put("j", "1");
            keyValuePairs.add(a);
    
            HashMap<String, String> e = new LinkedHashMap<String, String>();
            e.put("a", "1"); e.put("b", "1"); e.put("d", "1"); e.put("e", "1"); e.put("f", "1"); e.put("h", "1"); e.put("i", "1"); e.put("j", "1");
            keyValuePairs.add(e);
    
            HashMap<String, String> b = new LinkedHashMap<String, String>();
            b.put("a", "1"); b.put("b", "1"); b.put("c", "1"); b.put("d", "1"); b.put("e", "1"); b.put("f", "1"); b.put("j", "1"); b.put("k", "1"); b.put("l", "1");
            keyValuePairs.add(b);
    
            HashMap<String, String> c = new LinkedHashMap<String, String>();
            c.put("a", "1"); c.put("b", "1"); c.put("x", "1"); c.put("d", "1"); c.put("e", "1"); c.put("f", "1"); c.put("g", "1"); c.put("h", "1"); c.put("m", "1");
            keyValuePairs.add(c);
    
            // create a map to store the max and min indices
            Map<String, Integer[]> m = new HashMap<String, Integer[]>();
    
            Integer curpos = new Integer(0);
    
            // loop through the data and find the max and min indices of each data (key)
            for ( Map<String,String> s : keyValuePairs) {
                curpos = 0;
                for ( String t : s.keySet() ) {
    
                    if ( !m.containsKey(t) ){
                        // if its the first time to see the data, just add its current index as max and min
                        m.put(t,new Integer[] {curpos, curpos});
    
                    }
                    else {
                        // check if index is lower than existing minimum
                        Integer[] i = m.get(t);
                        if ( i[0] > curpos) {
                            m.put(t, new Integer[] {curpos, i[1]});
                        }
                        //check if index is greater than current maximum
                        if ( curpos > i[1] ) {
                            m.put(t, new Integer[] {i[0], curpos});
                        }
                    }
                    curpos++;
                }
            }
    
            System.out.println("The unsorted data");
    
            for ( HashMap<String,String> h : keyValuePairs ) {
                for ( String s : h.keySet() ) {
                    System.out.print(" " + s + " ");
                }
                System.out.println();
            }
            System.out.println("\n");
    
            // Sort the data using our custom comparator
            ValueComparator com = new ValueComparator(m);
            List<String> toSort = new LinkedList<String>(m.keySet());
            Collections.sort(toSort, com);
    
            System.out.println("The sorted data");
    
            for ( HashMap<String,String> h : keyValuePairs) {
    
                for ( String s : toSort ) {
                    if ( h.containsKey(s) ) {
                        System.out.print(s + " ");
                    }
                    else {
                        System.out.print("  ");
                    }
                }
                System.out.println();
            }
    
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following data test<-data.frame(group=1:10, var.a=rnorm(n=10,mean=500,sd=20), var.b=runif(10)) I would like a barplot with
I would like to transform the following lines of data into a structured table
I have the following data that looks like this for example: 34 foo 34
I have following data in my table. alt text http://img26.imageshack.us/img26/3746/productfield.png I want to extract
I have following data in excel table r1 r2 r3 r4 r5 v1 v2
I would like to output the following table from my database DateTime | Measure
I have following data in database: 1 2 3 4 5 And I would
I have the unfortunate task of having to import data from excel into a
I would like to write into a csv file using PHPEXCEL in the following
I have following data frames > head(elo) date elo 1 1921-12-18 1597 2 1922-05-14

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.