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 8980797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:10:33+00:00 2026-06-15T20:10:33+00:00

I have a problem sorting the contents of a 2D integer array (not ArrayList)

  • 0

I have a problem sorting the contents of a 2D integer array (not ArrayList) in Java. My problem array path_info[ ][ ] looks something like this:

Node_x Node_y Path_ID Port_x Port_y
  4      6      500     3       2
  6      8      500     3       2      
  4      9      501     2       3
  9      3      501     2       2      
  2      3      502     3       2      
  1      5      503     2       3 
  5      2      503     2       2

Each row means: node_x to node_y on Path_ID through Port_x to Port_y. Please note, each path can be one or more rows in the table.

This array is the resultant array from a routing algorithm to reach node 1 from node 8 on an undirected unweighted graph.

To reach from a source node to a destination node; for example node 1 from node 8 in the table, the path_IDs are 500, 501, 502 and 503 (PATH_IDs already sorted in column Path_ID). The problem is that I want this array to be sorted in such a way that the source node is the first node of the column “Node_x” and the destination node is the last node .of the column “Node_y”. And all the intermediate rows and columns sorted appropriately.

The resultant array should look like this (when source node is 8 and destination node is 1):

Node_x Node_y Path_ID Port_x Port_y
  8      6      500     2      3
  6      4      500     2      3     
  4      9      501     2      3
  9      3      501     2      2
  3      2      502     2      3
  2      5      503     2      2
  5      1      503     3      2

I have not started writing code yet so do not have a snippet to paste. I am still trying to figure out how to achieve this. Could someone please help me?

  • 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-15T20:10:35+00:00Added an answer on June 15, 2026 at 8:10 pm

    Assuming your algorithm worked correctly, you shouldn’t ever be visiting the same node more than once. So you can just greedily look for the right nodes, but you need a good data structure so you don’t loop incessantly. This code assumes that path_info[index][0] is node_x, and path_info[index][1] is node_y. Try something like this, which runs in O(n) time for each loop:

    import java.util.*;
    
    public class GraphSorter {
      public static void main(String[] args) {
        int[][] graph = new int[][]{{9, 3}, {6, 4}, {3, 2}, {8, 6}, {5, 1}, {4, 9}, {2, 5} }; 
        LinkedList<int[]> myList = path(graph, 8);
        for (int[] edge : myList) {
          System.out.println(Arrays.toString(edge));
        }
      }
    
      public static LinkedList<int[]> path(int[][] path_info, int source) {
        HashMap<Integer, int[]> nodeXmap = new HashMap<Integer, int[]>();
        HashMap<Integer, int[]> nodeYmap = new HashMap<Integer, int[]>();
        LinkedList<int[]> foundPath = new LinkedList<int[]>();
    
        for(int i = 0; i < path_info.length; i++) {
          // We already found an edge with this node_x edge, turn it around
          if(nodeXmap.containsKey(path_info[i][0])) {
            int tmp = path_info[i][0];
            path_info[i][0] = path_info[i][1];
            path_info[i][1] = tmp;
          }
          nodeXmap.put(path_info[i][0], path_info[i]);
          nodeYmap.put(path_info[i][1], path_info[i]);
        }
    
        int current = source;
        // Use our maps to lookup where the next edge exists in our path,
        // since our input is guaranteed to be unique
        for(int i = 0; i < path_info.length; i++) {
          int[] currentEdge = nodeXmap.get(current);
          if (currentEdge == null) {
            currentEdge = nodeYmap.get(current);
            current = currentEdge[0];
          } else {
            current = currentEdge[1];
          }
          foundPath.add(currentEdge);
          nodeXmap.remove(currentEdge[0]);
          nodeYmap.remove(currentEdge[1]);
        }
    
        return foundPath;
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem with sorting this array: Array ( [0] => stdClass Object (
I have an integer array sorting program here but I have a problem: whenever
I have a problem with sorting NSTableColumn contents. In my NSTableView there are three
I have a problem sorting vectors in 2D vector? I would like to sort
I got problem for sorting array of nsdate. so i have array of date
I have a problem with sorting an ArrayList of my own objects. My class
I have a problem with sorting of an array. $infoGroup is the result of
I have problem in sorting an array in ascending form and i don't know
I have a problem sorting join entities with CoreData. I've made a model like
I have a problem with sorting... I need to sort NSArray containing NSDictionaries .

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.