Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Sortting an array

Posted on 2009-07-08
6
278 Views
Last Modified: 2013-12-25
I have a list of names like this.

[0]Bob
[1]Joe
[2]Bob
[3]Bob
[4]Bob
[5]Joe
[6]Phil
[7]Phil

I want to sort my array without changing it so that its like this (so basically give an alternative index of elements so its like this):

[0]Bob
[2]Bob
[3]Bob
[4]Bob
[1]Joe
[5]Joe
[6]Phil
[7]Phil

#Class contains the name information.
 
$m=0;
$no=0;
$kl =0;
@keep=();
 
foreach (@Class){
	$test = $no;
		foreach $u (0..$#keep){
			if ($_=~m/$keep[$u]/){
				$no++;
			} elsif ($_ =~ m/^[ \t]*$/){
				$Class[$m]="CHECK MANUALLY";
			}
		}
		if ($no==$test){
		$keep[$kl] = $_;
		$kl++;
		}
	$m++;
}
 
@keep2=();
@index =();
$indcount=0;
foreach $hu (@keep){
 
	foreach $ij (0..$#Class){
		if ($Class[$ij] =~ m/$hu/){
			$index[$indcount]=$ij;
			$indcount++;
		}
	}
	
}

Open in new window

0
Comment
Question by:sanjangeorge
6 Comments
 
LVL 13

Expert Comment

by:kawas
ID: 24804265
To sort an array in ASCII (Alphabetical) order :
sort(@array);

Example of storing a sort result back into the same array :
@array = sort (@array);

To sort an array in reverse ASCII (Alphabetical) order :
sort {$b cmp $a} (@array);

To sort an array in numeric ascending order :
sort {$a <=> $b} (@array);

To sort an array in numeric decending order :
sort {$b <=> $a} (@array);

To print a sorted array but not alter the original :
print sort(@array);

To reverse the value elements in an array :
reverse (@array);


from http://www.htmlite.com/perl016.php
0
 

Author Comment

by:sanjangeorge
ID: 24804300
Yeah i've seen things like this not quite what im looking for
0
 
LVL 13

Expert Comment

by:kawas
ID: 24804351
if you are trying to sort the arrays and keep indices (old and new) maybe the data structure you are using isnt correct for you.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 11

Expert Comment

by:climbgunks
ID: 24804470

#!/usr/bin/perl -w
use strict;
 
my @unsorted = (
             "[0]Bob",
             "[1]Joe",
             "[2]Bob",
             "[3]Bob",
             "[4]Bob",
             "[5]Joe",
             "[6]Phil",
             "[7]Phil"
             );
 
my @sorted = sort {
   $a =~ /^\[(\d+)\](.*)$/ or die "input $a not in expected form\n";
   my ($a1, $a2) = ($1, $2);
   $b =~ /^\[(\d+)\](.*)$/ or die "input $b not in expected form\n";
   my ($b1, $b2) = ($1, $2);
   $a2 eq $b2 ? $a1 <=> $b1 : $a2 cmp $b2;
} @unsorted;
 
print join("\n", @sorted)."\n";

Open in new window

0
 
LVL 39

Accepted Solution

by:
Adam314 earned 500 total points
ID: 24804571

my @Unsorted = qw(Bob Joe Bob Bob Bob Joe Phil Phil);
print "array=\n" . join("",map {"    [$_]$Unsorted[$_]\n"} (0..$#Unsorted));
 
my @indexes = sort {$Unsorted[$a] cmp $Unsorted[$b]} (0..$#Unsorted);
print "array=\n" . join("",map {"    [$_]$Unsorted[$_]\n"} @indexes[0..$#Unsorted]);

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
ID: 24806972
my @unsorted = (
             "[0]Bob",
             "[1]Joe",
             "[2]Bob",
             "[3]Bob",
             "[4]Bob",
             "[5]Joe",
             "[6]Phil",
             "[7]Phil"
             );
$\=$/;
print for @sorted = map{/\0(.*)/}sort map{join"\0",(/([][\d]*(.*))/)[1,0]} @unsorted ;
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question