In my application, I let users choose how they want their data arranged (sorted or unsorted).

When they choose sorted, I simply sort everything. That part is straightforward to me.

However, when they choose sorted, and then choose unsorted, I want to show them the data in the same order that the database gave it to me (i.e. I don't want to just call randomize() or something).

Is there an efficient way of "unsorting" an array that I sorted myself without maintaing a copy of the original array?

One thing you could do is keep an extra (hidden) integer value in each record which records the original order that you received the records from the database. When the user chooses "unsorted", then sort by that original-order value.

Why not just keep a copy of the original collection? Or just never modify it in place and instead generate sorted copies of the original collection as the user requests sorting. This seems like much simpler than keeping an original-order value or something like that.

Similar Questions

This question already has an answer here: The concise way to initialize an array of reference type object 3 answers I would like to initialise an array of List with empty List in the most effic

I have an array of Sorted integers. We can use binary search to find an element . Now if one element of sorted array is interchanged with another element. What would be the best way to find the interc

I apologize if this has been asked here - I've hunted around here and in the Tentative NumPy Tutorial for an answer. I have 2 numpy arrays. The first array is similar to: 1 0 0 0 0 2 0 0 0 0 3 0 0 0 0

Given a sorted array A[1...n] of keys, and another key, x, stored in A, show how to find the index, k, so that A[k] = x in time O(log(k)). I know that a binary search on a sorted array would be comple

So say I have an array that looks like this: links = [['May 1', 'Link A', 'www.linka.com'], ['May 2', 'Link B', 'www.linkb.com']] What I would like to do with Nokogiri is go to each link and return s

I would like to write a piece of code for inserting a number into a sorted array at the appropriate position (i.e. the array should still remain sorted after insertion) My data structure doesn't allow

I need to transform an array to html table sorted by time, add rank and calculate time loss for the first place. Array example:` var array = [ [McNulty Oscar, USA, 108:45.1], [Johansson Anton,

Is there any efficient way to calculate 2x2 matrix H without for statement? n=10 a=array(rnorm(n),c(2,1,n)) b=array(rnorm(n),c(2,1,n)) H=matrix(0,2,2) for(i in 1:n) H=H+a[,,i] %*% t(b[,,i])

im having table with more than 1k rows and more than 7 columns, trying to parse into array object, i tried using jquery $(tableSearch).each(function () { $('tr', $(this)).each(function (key, tr) { va

We have a sorted array and we would like to increase the value of one index by only 1 unit (array[i]++), such that the resulting array is still sorted. Is this possible in O(1)? It is fine to use any

Is there a more efficient way to do the following? $(this).parent().parent().parent().parent().parent().addClass('finished'); Note that I need the parent concept, because I have multiple similar item

I want to save my objects according to a key in the attributes of my object in a sorted fashion. Later on I'll access these objects sequentially from max key to min key. I'll do some search tasks as w

how to read a file in an efficient way in java?? suggest any methods.. i used BufferedReader,which is very slow.. suggest any changes?? CODE: import java.io.*; import java.awt.*; import javax.swing.*;

Possible Duplicate: Fastest strategy to form and sort an array of positive integers What would be the fastest way to get a sorted array from an unsorted iterable of integers ? Currently I do it by i

I know there are at least two ways. One way is to every time you add an edge to the acyclic tree, traverse necessary vertices. The downside is as the tree grows big, the traverse procedure takes more

I am trying to optimize a search through a very short sorted array of doubles to locate a bucket a given value belongs to. Assuming the size of the array is 8 doubles, I have come up with the followin

I have a document with the following structure: { tags: ['bbb', 'aaa', 'ddd', 'ccc'] } and I want to create a view which returns the hash of the sorted tags array as key. My approach is to sort the t

I am making making an application in vb.net that will read files using a byte array as buffer, and create parity files for them by xoring the data... What would be the most efficient way of xoring a b

I won't repeat my question here, but is there are more efficient way to write this? def recruits_names names = [] for r in self.referrals do names << r.display_name end return names end

I have an array of sorted ints with a 1,000 or more values (could be up to 5000+). I need to write a function that receives an int and returns a bool based on the element being in the array. I know I

I need a fast and efficient method to read a space separated file with numbers into an array. The files are formatted this way: 4 6 1 2 3 4 5 6 2 5 4 3 21111 101 3 5 6234 1 2 3 4 2 33434 4 5 6 The fi

Quick question: I have an array double[]array=new double [10] in which there are random doubles, say between 0 and 20. What i want is get another array int []resultingArray=new int [array.length] that

Internet seems to be full of similar (solved) questions. But non of these realy fit. I have an Array of Strings and want to count the occurancies of any single String. I have already sorted it. (Its a

I have a Java ObservableList with thousands of entries that receives hundreds of updates a second backing a JavaFX TableView. The ObservableList is backed by an ArrayList. Arbitrary sort orders can b

Underscore provides the function sortBy to sort an array of objects. However once I have this sorted array, is there a way to find an element using binary search? The function find does not leverage t

Say you have a 2D grid of tiles (This is for a 2D tile based game), most tiles occupy 1 spot, however some larger objects can fill in multiple spots. I use an indexer on my array to automatically r

The problem: Consder the following floats[]: d[i] = 1.7 -0.3 2.1 0.5 What I want is an array of int[] that represents the order of the original array with indices. s[i] = 1 3 0 2 d[s[i]] = -0.3 0.5 1

I have a nested list with different list sized and types. def read(f,tree,objects): Event=[] for o in objects: #find different features of one class temp=[i.GetName() for i in tree.GetListOfBranches(

I was asked this question in an interview and I gave various solutions but the interviewer was not convinced. I am interested to find a solution. Please throw in your views : Q: Write an efficient da

Let's say I have an Array ary = [0.0, 1.0, 5.0, 1.0, -2.0, 3.5], and I want as output another array of the same size containing ary's indices in sorted-by-value-order. In other words, the output shoul

I am looking to print out my original unsorted array, I have it printing in order and sorted but I can't seem to get the original one to print out unsorted. I have used printRuleAndArray(String rule)

I have 2 arrays Array 1 : $agents = Array('abc','xyz','pqr'); Array 2 : $tot_calls = Array ('10','5','20'); Here array 2 reflects total calls made by agents in $agents array respectively. i.e Agent a

My goal is to randomly sort an array and to display the sorted information in a text box. Simple enough, I would think (but not so simple for someone such as myself, unfortunately). Anyway, here's wha

Pretty simple scenario. I have a web service that receives a byte array that is to be saved as a particular file type on disk. What is the most efficient way to do this in C#?

I got this as an interview question ... infinite array which is sorted and from some position (we dont know the position) only special symbol '$' will be there we need to find an element in that array

In MongoDB, what's the most efficient approach to sort an array in a document and then store the sorted array back to the document? For background, I have a process that generates a collection of time

I need to insert a value into an array... ideally I could just start with List<myObj>, but the methods I need to use return myObj[] instead. I always need to insert a value into the first posit

I've written the following program to implement Binary Search of a sorted array: int flag=0; void binarysearch(int x, int a[], int m, int n) { int middle=(m+n)/2; if(a[middle]==x) { printf(%d has be

I have a chance to learn how to administer and develop for WebSphere. But after trying to find a book for it I am at a loss where to begin. I was hoping for some advice. I may have access to the IBM R

I have a 2 dimensional array. The rows and columns are sorted. How to find the kth largest element from the 2-d array?

I am writing a Huffman encoding/decoding tool and am looking for an efficient way to store the Huffman tree that is created to store inside of the output file. Currently there are two different versio

Am I doing this right? Will this help avoid sql injections? $deleteid = htmlspecialchars(strip_tags(mysql_real_escape_string($_POST['listid']))); mysql_send(DELETE FROM stage where listid='$deleteid'

I have an array containing several keys, values, objects etc.. I need to empty that array but I'd like to do it in the most efficient manner. The best I can come up with is: foreach ($array as $key =&

I need to find the position( or index ) say i of an integer array A of size 100, such that A[i]=0. 99 elements of array A are 1 and only one element is 0. I want the most efficient way solving this pr

Possible Duplicate: How do i count same values in an array and store it to a variable? I want to know if there's any efficient way to count repetitions on a PHP int array. In a very big large array

If I wanted to get a list of product_ids with a certain brand. I would do this: $id_list = array(); $qry = 'SELECT product_id FROM products WHERE product_brand = :brand'; $STH = $this->pdo->prep

I have 2 set of unsorted integers: set A and set B. But we don't know how many items are there in setB in advance. I need to : while setA and setB are not empty: pop the smallest no from setA move an

I have a question regarding the recommended/best practice for managing a large array of bitmaps. (It's a flipbook type app where the user goes through and creates new pages...like a virtual pile of st

On sorting an array i get : 1,10,2,3,4,5,6,7,8,9. What went wrong ? My code was: NSArray *sortedArray = [optionKeys sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; where optio

Say you're given the array containing: Jordan Jord Anna Rob RobJord And you want to return an array of all values that contain Jord (i.e Jord, Jordan, RobJord), what is the most efficient way to do th